How To Highlight Selected Dates In Date Picker

后端 未结 2 816
孤街浪徒
孤街浪徒 2021-01-06 20:03

First is there a way to not highlight the current day. Second is there a way to highlight specific days like the way the current day was highlighted?

Here is the co

相关标签:
2条回答
  • 2021-01-06 20:30

    First is there a way to not highlight the current day.

    Yes, you can take away the highlighting by removing .ui-state-highlight and .ui-state-hover classes from the anchor element.

    Second is there a way to highlight specific days like the way the current day was highlighted?

    Yes, either add the classes for highlighting the current day to those days you want to have highlighted, or override the CSS with .ui-state-highlight's style.

    For the first approach, here's an example code snippet:

    $('#datepicker').datepicker({
        numberOfMonths: [1, 1],
        beforeShowDay: highlightDays
    }).click(function() {
        // question 1
        $('.ui-datepicker-today a', $(this).next()).removeClass('ui-state-highlight ui-state-hover');
    
        // question 2
        $('.highlight a', $(this).next()).addClass('ui-state-highlight');
    });
    

    See the first approach in action: http://jsfiddle.net/william/Aut9b. See the second approach in action: http://jsfiddle.net/william/Aut9b/1/.

    0 讨论(0)
  • 2021-01-06 20:33

    Use beforeShowDay option of jQuery UI date-picker to highlight selected dates in date-picker. Here the simple code:

    $( function() {
        // An array of dates
        var eventDates = {};
        eventDates[ new Date( '08/07/2016' )] = new Date( '08/07/2016' );
        eventDates[ new Date( '08/12/2016' )] = new Date( '08/12/2016' );
        eventDates[ new Date( '08/18/2016' )] = new Date( '08/18/2016' );
    
        // datepicker
        $('#datepicker').datepicker({
            beforeShowDay: function( date ) {
                var highlight = eventDates[date];
                if( highlight ) {
                    return [true, "event", 'Tooltip text'];
                } else {
                    return [true, '', ''];
                }
            }
        });
    });
    

    The above JavaScript will add event class on selected dates. Now you will need to define the style for selected dates. For the complete guide, you can check the source link mentioned above.

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