Juery基础 专题
您的位置:JS框架 > Juery基础专题 > jQuery Prettydate
jQuery Prettydate
作者:--    发布时间:2019-11-20

jquery prettydate 插件为表单提供了强大的验证功能,让客户端表单验证变得更简单,同时提供了大量的定制选项,满足应用程序各种需求。该插件捆绑了一套有用的验证方法,包括 url 和电子邮件验证,同时提供了一个用来编写用户自定义方法的 api。所有的捆绑方法默认使用英语作为错误信息,且已翻译成其他 37 种语言。

该插件目前版本是 1.1.0。

访问 jquery prettydate 官网,下载 jquery prettydate validation(密码验证)插件。

使用方式

如需使用 prettydate 插件,您需要在 title 中带有 iso8601 日期:

<a title="2008-01-28t20:24:17z">january 28th, 2008</a> 
<a title="2008-01-27t22:24:17z">january 27th, 2008</a> 
<a title="2008-01-26t22:24:17z">january 26th, 2008</a>

然后对它们应用 prettydate 方法:

$(function() { $("a").prettydate(); });

如需本地化该插件,请在 $.prettydate.messages 中重写属性。在这里,以德国本地化为例:

$.prettydate.messages = { 
now: "gerade eben", 
minute: "vor einer minute", 
minutes: $.prettydate.template("vor {0} minuten"), 
hour: "vor einer stunde", 
hours: $.prettydate.template("vor {0} stunden"), 
yesterday: "gestern", 
days: $.prettydate.template("vor {0} tagen"), 
weeks: $.prettydate.template("vor {0} wochen") 
}

该插件每隔 10 秒中更新一次每个被选中的元素。这样子 "just now" 会变为 "1 minute ago" 再变为 "x minutes ago" 再变为 "1 hour ago" 等等。

您可以通过指定 interval 选项为 "false" 来禁用间隔更新:

$(function() { $("a").prettydate({ interval: false }); });

或者设置一个不同的时间间隔,例如:interval: 1000,每隔一秒更新一次每个被选中的元素:

$(function() { $("a").prettydate({ interval: 1000 }); });

value 选项默认读取 title 属性中的 iso8601 日期字符串。重载该选项来使用其他属性,例如,一个自定义的 "isodate" 属性:

$(function() { $("a").prettydate({ function() { // "this" is the dom element return $(this).attr("isodate"); } }); });

实例演示

jquery prettydate 插件演示。本实例使用了一个固定的日期,因为该插件不会格式化一个月之前的日期。实例不使用类似 "6 months ago" 这种模糊的表达,而是保持原有的日期字符串。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>jquery prettydate 插件</title>
 <script  class="lazy" data-original="http://jquery.bassistance.de/prettydate/libs/jquery.js"></script>
  <script  class="lazy" data-original="http://jquery.bassistance.de/prettydate/jquery.prettydate.js"></script>
    <style>
 * {
       margin: 0;
        padding: 0;
   }
 body {
        background: #eee;
     font: 14px/21px sans-serif;
       color: #333;
      position: relative;
   }
 h1 {
      text-align: center;
       padding: 1em 0;
   }
 a {
       text-decoration: none;
        color: #0645ad;
   }
 a:hover {
     text-decoration: underline;
   }
 fieldset, p {
     margin: 0.5em 0;
  }
 .fork {
       position: fixed;
      top: 0; right: 0;
     background: url(https://a248.e.akamai.net/assets.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67) no-repeat 100% 0;
        width: 150px;
     height: 150px;
        float: right;
 }
 .entries {
        margin: 0 auto 3em;
       padding: 0.5em 0.5em 0;
       background: #fff;
     width: 500px;
     overflow: hidden;
 }
 fieldset, .entry {
        background: #f2f2f2;
      border: 1px solid #ddd;
       padding: 0.25em 0.5em;
        margin-bottom: 0.5em;
     list-style: none;
     overflow: hidden;
 }
 .entry .extra {
       float: right;
 }
 </style>
    <script>
        jquery(function ($) {
         // 通过 javascript 生成演示,确保 html 不重复
         var zulus, $langs,
                msgsen = $.extend({}, $.prettydate.messages),
             $langmenu = $('<select>'),
                $entries = $('.entries ul').empty();


            function dopretty() {
             $('.time').prettydate({
                 // 由于时间是固定的,不需要更新
                 interval: false
               });
           }

           zulus = [
             '2012-01-01t00:00:00z',
             '2008-01-28t22:24:30z',
             '2008-01-28t22:23:05z',
             '2008-01-28t22:20:05z',
             '2008-01-28t20:24:17z',
             '2008-01-27t08:00:00z',
             '2008-01-26t08:00:00z',
             '2008-01-21t08:00:00z',
             '2008-01-14t08:00:00z',
             '2007-12-28t08:00:00z',
             '2007-12-15t08:00:00z',
             '2007-01-14t08:00:00z',
             '2006-03-07t08:00:00z'
          ];
            $.each(zulus, function (i, zulu) {
                $entries.append(
                  $('<li>', {
                       addclass: 'entry',
                      html: '<a href="#' + zulu + '">blah blah blah</a>' +
                            ' <small class="extra"><span class="time" title="' +                            zulu + '">' + new date($.prettydate.parse(zulu)).todatestring() + '</span>' +
                           ' &nbsp;&bull;&nbsp; ' + '<a class="author" href="#/author/john/">john resig</a></small>'
                   })
                );

          });


           $langs = $.map([
              'cn',
               'da',
               'de',
               'es',
               'fr',
               'id',
               'lv',
               'nl',
               'pl',
               'pt-br',
                'sv',
               'th',
               'tr'
            ], function (lang) {
              return $('<option>').text(lang).prop('value', lang);
            });
           $langs.unshift(
               $('<option>').text('en').prop({
                 value: '',
                  selected: true
                })
            );

          $langmenu
             .prop('id', 'lang-menu')
              .append($langs)
               .on('change', function () {
                 if (this.value !== '') {
                        // $.getscript,但带有缓存
                      $.ajax({
                          url: 'http://jquery.bassistance.de/prettydate/i18n/jquery.prettydate-' +  this.value + '.js',
                         datetype: 'script',
                         cache: true
                       }).done(dopretty);
                    } else {
                      $.prettydate.messages = msgsen;
                       dopretty();
                   }
             })
                .wrap('<fieldset>')
                   .parent()
                 .prependto('.entries');

           // 独立于当前日期的固定日期
           // (防止变成“5 years ago...”)
         $.prettydate.now = function () {
              return new date(1201559100000);
           }

           $('<p>').text('local time: ' + $.prettydate.now()).prependto('.entries');

           dopretty();
       });
   </script>
</head>
<body>
  <a class="fork" href="https://github.com/jzaefferer/jquery-prettydate" title="fork jquery.prettydate on github!"></a>
   <h1>jquery prettydate 插件演示</h1>
   <div class="entries">
     <ul><em>加载...</em></ul>
       <hr>
        <p><a href="https://github.com/jzaefferer/jquery-prettydate">源代码:github.com/jzaefferer/jquery-prettydate</a></p>
    </div>
</body>
</html>


网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
373515719@qq.com
关于本站:
编程参考手册