簡體   English   中英

jQuery UI Datepicker beforeShowDay函數無法運行

[英]jQuery UI Datepicker beforeShowDay function doesn't get run

我使用jQuery的datepicker作為日歷,我試圖使用函數'beforeShowDay'突出顯示日歷中的某個日期,但該函數甚至沒有被觸發。

JavaScript的

        jQuery(document).ready(function() {
            // An array of dates
            var eventDates = [new Date(2016, 9, 7),new Date(2016, 9, 8)];
            console.log(('#calendar'.beforeShowDay));
            console.log(eventDates);
            // datepicker
            $('#calendar').datepicker({
                dateFormat: 'yy-mm-dd',
                beforeShowDay: function(date) {
                    var highlight = eventDates[date];
                    console.log(highlight);
                    if(highlight) {
                        return [true, "calendarHighlight", ''];
                    } else {
                        return [true, '', ''];
                    }
                }
            });
        });

CSS

.calendarHighlight {
            background-color: red; !important;
        }

HTML

<div id="calendar"></div>

您的問題是您將值存儲在數組中,但嘗試像對象一樣訪問它

排列

var eventDates = [new Date(2016, 9, 7),new Date(2016, 9, 8)];

這不行

var highlight = eventDates[date];

您還應該使用.getTime()比較日期

那樣做

var eventDates = [new Date(2016, 9, 7).getTime(),new Date(2016, 9, 8).getTime()];

然后使用jQuery.inArray檢查它是否在數組中

var highlight = $.inArray(date.getTime(),eventDates) >= 0;

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM