使用jQuery的datepicker突出显示特定范围内的日期

19 jquery date datepicker highlight

我需要突出显示开始日期和结束日期之间的日期,我应该能够指定.谁能帮我?

Dav*_*her 33

您可以使用beforeShowDay事件.每个需要在日历中显示的日期都会调用它.它传递一个日期并返回一个数组,其中[0] = isSelectable,1 = cssClass,[2] =一些工具提示文本

$('#whatever').datepicker({
            beforeShowDay: function(date) {
             if (date == myDate) {
              return [true, 'css-class-to-highlight', 'tooltipText'];

              }
           }
});
Run Code Online (Sandbox Code Playgroud)

  • 如果您不想突出显示某一天,请不要忘记返回[true,'']:http://stackoverflow.com/a/9358119/664132 (2认同)

Mik*_*ike 24

这是一个有效的例子!你将需要通过http://jqueryui.com/download与核心,小部件和日期选择器一起制作一个软件包.

之前的javascript部分:

<script>
$(document).ready(function() {

    var dates = ['22/01/2012', '23/01/2012']; //
            //tips are optional but good to have
    var tips  = ['some description','some other description'];      

    $('#datepicker').datepicker({                
        dateFormat: 'dd/mm/yy',
        beforeShowDay: highlightDays,
        showOtherMonths: true,
        numberOfMonths: 3,
    });

    function highlightDays(date) {
        for (var i = 0; i < dates.length; i++) {
            if (new Date(dates[i]).toString() == date.toString()) {              
                return [true, 'highlight', tips[i]];
            }
        }
        return [true, ''];
     } 

});
</script>
Run Code Online (Sandbox Code Playgroud)

HTML部分:

<div id="datepicker"></div>
Run Code Online (Sandbox Code Playgroud)

添加这个CSS的地方:

    td.highlight {border: none !important;padding: 1px 0 1px 1px !important;background: none !important;overflow:hidden;}
td.highlight a {background: #99dd73 url(bg.png) 50% 50% repeat-x !important;  border: 1px #88a276 solid !important;}
Run Code Online (Sandbox Code Playgroud)

你需要制作一个名为bg.png的小图像才能使它工作


Mar*_*phy 13

以为我会投入两美分,因为它似乎比其他人更快,更轻盈:

jQuery(function($) {
  var dates = {
    '2012/6/4': 'some description',
    '2012/6/6': 'some other description'
  };

  $('#datepicker').datepicker({
    beforeShowDay: function(date) {
      var search = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();

      if (search in dates) {
        return [true, 'highlight', (dates[search] || '')];
      }

      return [false, '', ''];
    }
  });
});
Run Code Online (Sandbox Code Playgroud)