jQueryUI Datepicker组件设置日期高亮


最近在看JQueryUI Datepicker组件的时候想到有时候我们需要高亮某些日期,而不仅仅是当前日期和选中的日期,这是我们就需要在日历组件初始化的时候给某些日期设置成高亮,以表示这些日期和其它日期有区别,比如说可以表示这些日期有一些meeting或者task。对于这种需求可以通过使用组件的beforeShowDay(date)函数来实现,这个函数会在Datepicker组件初始化的时候对于每一天都调用一次这个函数来做一些定制的功能,从而正好可以实现我们所要的需求。

下面来看怎样实现

首先下载jquery-ui-1.11.1包,并解压。

然后在jquery-ui-1.11.1目录下创建一个calenar.html文件用来测试。

calenar.html的内容如下:

<!doctype html>
<html lang="us">
<head>
 <meta charset="utf-8">
 <title>jQuery UI Example Page</title>
 <link href="jquery-ui.css" rel="stylesheet">
 <style>
 td.highlight {border: none !important;padding: 1px 0 1px 1px !important;background: none !important;overflow:hidden;}
 td.highlight a {background: #AABBCC !important; border: 1px #88a276 solid !important;}
 </style>
 <script src="external/jquery/jquery.js"></script>
 <script src="jquery-ui.js"></script>
 <script>
 $(function() {
  $( "#datepicker" ).datepicker({
  inline: true,
  showButtonPanel: true,
  onSelect: function (dateText, inst) {
   alert(dateText);
  },
  beforeShowDay: function(date) {
   var dates = ['09/01/2014', '09/02/2014', '10/01/2014'];
   var tips = ['09/01/2014', '09/02/2014', '10/01/2014'];
   for (var i = 0; i < dates.length; i++) {
   if (new Date(dates[i]).toString() == date.toString()) {
    return [true, 'highlight', tips[i]];
   }
   }
   return [true, ''];
  }
  });
 });
 </script>
</head>
<body>
<div id="datepicker"></div>
</body>
</html>

其中beforeShowDay函数定义了需要高亮的三个日期,当初始化的日期等于这个日期中的一个的时候,设置这个日期为高亮,否则返回默认值。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持积木网。

浅谈jquery上下滑动的注意事项
div:divid="pay_box"class="vip_bottom"fffff/divstyle:.vip_bottom{position:fixed;display:none;//注意,先不显示,点击某个div后从上向下滑动,默认时none,这样才会有滑动效果

jQuery旋转插件jqueryrotate用法详解
本文实例讲述了jQuery旋转插件jqueryrotate用法。分享给大家供大家参考,具体如下:CSS3提供了多种变形效果,比如矩阵变形、位移、缩放、旋转和倾斜等

使用jquery给指定的table动态添加一行、删除一行
需求场景1)、添加一行支持在任意行添加一行且可配置的,如可在第一行添加一行、第二行添加一行、倒数第一行添加一行、倒数第二行添加一行,随