Highlight specific days of a week in jQuery datepicker

久未见 提交于 2019-12-25 01:45:16

问题


my aim is to highlight specific days of a week in jQuery datepicker.

For example I want to highlight Tuesdays and Thursdays. I've seen many examples how to highlight specific dates(see: http://jquerybyexample.blogspot.com/2012/05/highlight-specific-dates-in-jquery-ui.html). But not a single one that covers my needs.

Any hints?


回答1:


You could adapt the example you linked, to do what you want, like this:

CSS:

.Highlighted a{
   background-color : Green !important;
   background-image :none !important;
   color: White !important;
   font-weight:bold !important;
   font-size: 12pt;
}

Javascript:

$(document).ready(function() {
    $('#datepicker').datepicker({
        beforeShowDay: function(date) {
            var day = date.getDay();
            if (day == 2 || day == 4) {
                return [true, "Highlighted", date];
            }
            return [true, '', ''];
        }
    });
});​

that should do it.




回答2:


You could do this with only CSS using the + adjacent selector if you can assume the week starts on Sunday. It may look a little ridiculous but it will work in any browser that supports + and :first-child (Which includes IE7 and up).

/* Tuesday:                Su               Mo   Tu            */
.ui-datepicker-calendar tr td:first-child + td + td a,
/* Thursday:               Su               Mo   Tu   We   Th  */
.ui-datepicker-calendar tr td:first-child + td + td + td + td a {
    background:red;
}
​

Demo: http://jsfiddle.net/jnGhV/1/




回答3:


This should do it:

$(document).ready(function() {
    $('#txtDate').datepicker({
        beforeShowDay: function(date) {
            if(date.getDay() == 2 || date.getDay() == 4) {
                return [true, "Highlighted", ''];
            } else {
                return [true, '', ''];
            }

        }
    });
});​

JSFiddle: http://jsfiddle.net/XuExp/



来源:https://stackoverflow.com/questions/12567794/highlight-specific-days-of-a-week-in-jquery-datepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!