jQueryUI Datepicker组件设置日期高亮
最近在看JQueryUIDatepicker组件的时候想到有时候我们需要高亮某些日期,而不仅仅是当前日期和选中的日期,这是我们就需要在日历组件初始化的时候给某些日期设置成高亮,以表示这些日期和其它日期有区别,比如说可以表示这些日期有一些meeting或者task。对于这种需求可以通过使用组件的beforeShowDay(date)函数来实现,这个函数会在Datepicker组件初始化的时候对于每一天都调用一次这个函数来做一些定制的功能,从而正好可以实现我们所要的需求。
下面来看怎样实现
首先下载jquery-ui-1.11.1包,并解压。
然后在jquery-ui-1.11.1目录下创建一个calenar.html文件用来测试。
calenar.html的内容如下:
<!doctypehtml>
<htmllang="us">
<head>
<metacharset="utf-8">
<title>jQueryUIExamplePage</title>
<linkhref="jquery-ui.css"rel="stylesheet">
<style>
td.highlight{border:none!important;padding:1px01px1px!important;background:none!important;overflow:hidden;}
td.highlighta{background:#AABBCC!important;border:1px#88a276solid!important;}
</style>
<scriptsrc="external/jquery/jquery.js"></script>
<scriptsrc="jquery-ui.js"></script>
<script>
$(function(){
$("#datepicker").datepicker({
inline:true,
showButtonPanel:true,
onSelect:function(dateText,inst){
alert(dateText);
},
beforeShowDay:function(date){
vardates=['09/01/2014','09/02/2014','10/01/2014'];
vartips=['09/01/2014','09/02/2014','10/01/2014'];
for(vari=0;i<dates.length;i++){
if(newDate(dates[i]).toString()==date.toString()){
return[true,'highlight',tips[i]];
}
}
return[true,''];
}
});
});
</script>
</head>
<body>
<divid="datepicker"></div>
</body>
</html>
其中beforeShowDay函数定义了需要高亮的三个日期,当初始化的日期等于这个日期中的一个的时候,设置这个日期为高亮,否则返回默认值。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。