Is there a way to style jquery's datepicker days?

后端 未结 1 1205
没有蜡笔的小新
没有蜡笔的小新 2021-01-05 05:10

Let\'s say I want 5/2/2011 (or any other day) to be a different color than the rest of the days. Is this possible?

This is an example item for a particular day

相关标签:
1条回答
  • 2021-01-05 05:32

    Working Example: http://jsfiddle.net/hunter/UBPg5/


    You can do this by adding a beforeShowDay callback

    Bind your callback:

    $("input:text.date").datepicker({
        beforeShowDay: SetDayStyle
    });
    

    Create your function with some static array of bad dates or construct this array somewhere else:

    var badDates = new Array("5/2/2011");
    function SetDayStyle(date) {
        var enabled = true;
        var cssClass = "";
    
        var day = date.getDate();
        var month = date.getMonth() + 1; //0 - 11
        var year = date.getFullYear();
        var compare = month + "/" + day + "/" + year;
    
        var toolTip = badDates.indexOf(compare) + " " + compare
    
        if (badDates.indexOf(compare) >= 0) cssClass = "bad";
    
        return new Array(enabled, cssClass, toolTip);
    }
    

    Create your styles

    .bad { background: red; }
    .bad a.ui-state-active { background: red; color: white; }
    

    http://jqueryui.com/demos/datepicker/#event-beforeShowDay

    The function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name(s) or '' for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.

    0 讨论(0)
提交回复
热议问题