【问题标题】:Highlight specific dates in Jquery UI Datepicker在 Jquery UI Datepicker 中突出显示特定日期
【发布时间】:2012-04-27 17:49:12
【问题描述】:

我在这里阅读了其他类似的问题,但没有一个完全符合我的要求。

我想为两个日期之间的所有天添加一个类。日期将在变量中设置。

有什么想法吗?

【问题讨论】:

    标签: javascript jquery jquery-ui date jquery-ui-datepicker


    【解决方案1】:

    你需要实现beforeShowDay event for the datepicker:

    该函数将日期作为参数,并且必须返回一个数组 [0] 等于真/假,表示此日期是否为 可选择的,[1] 等于 CSS 类名或默认为 '' 演示文稿,以及 [2] 此日期的可选弹出工具提示。它是 日期选择器中的每一天在显示之前调用。

    所以你需要做类似的事情:

    $("#datepicker").datepicker({
        beforeShowDay: function(d) {
            var a = new Date(2012, 3, 10); // April 10, 2012
            var b = new Date(2012, 3, 20); // April 20, 2012
            return [true, a <= d && d <= b ? "my-class" : ""];
        }
    });
    

    演示:

    $(function() {
      $("#datepicker").datepicker({
        beforeShowDay: function(d) {
          // a and b are set to today ± 5 days for demonstration
          var a = new Date();
          var b = new Date();
          a.setDate(a.getDate() - 5);
          b.setDate(b.getDate() + 5);
          return [true, a <= d && d <= b ? "my-class" : ""];
        }
      });
    });
    @import url("https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/blitzer/jquery-ui.min.css");
    
    .my-class a {
      background: #FC0 !important;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
    
    <input id="datepicker">

    Another demo here

    【讨论】:

    • 效果很好!要记住的重要一点是,月份必须是负一,就像您在代码中所做的那样。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-08
    • 1970-01-01
    • 2019-12-26
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 2012-12-11
    相关资源
    最近更新 更多