我需要突出显示在开始日期和结束日期之间的日期,我应该能够指定这些日期。有人能帮我吗?
我需要突出显示在开始日期和结束日期之间的日期,我应该能够指定这些日期。有人能帮我吗?
您可以使用beforeShowDay事件。它将为需要在日历中显示的每个日期调用。它传递一个日期并返回一个数组,其中[0] = isSelectable,[1] = cssClass,[2] =某些工具提示文本
$('#whatever').datepicker({
beforeShowDay: function(date) {
if (date == myDate) {
return [true, 'css-class-to-highlight', 'tooltipText'];
}else{
//this will allow the cell be selected without been highlighted
return [true,'']
}
}
});
这是一个可用的示例!您需要使用http://jqueryui.com/download下载core,widget和datepicker以制作一个包。
放置在JavaScript部分之前:
<script>
$(document).ready(function() {
var dates = ['22/01/2012', '23/01/2012']; //
//tips are optional but good to have
var tips = ['some description','some other description'];
$('#datepicker').datepicker({
dateFormat: 'dd/mm/yy',
beforeShowDay: highlightDays,
showOtherMonths: true,
numberOfMonths: 3,
});
function highlightDays(date) {
for (var i = 0; i < dates.length; i++) {
if (new Date(dates[i]).toString() == date.toString()) {
return [true, 'highlight', tips[i]];
}
}
return [true, ''];
}
});
</script>
HTML代码:
<div id="datepicker"></div>
在某处添加以下CSS:
td.highlight {border: none !important;padding: 1px 0 1px 1px !important;background: none !important;overflow:hidden;}
td.highlight a {background: #99dd73 url(bg.png) 50% 50% repeat-x !important; border: 1px #88a276 solid !important;}
你需要制作一个名为 bg.png 的小图片才能使其正常工作
.highlight a { background: red; }
现在两者都会是红色的 :) - Mike我觉得自己也来说一下,因为这个方法似乎比其他方法更快、更轻便:
jQuery(function($) {
var dates = {
'2012/6/4': 'some description',
'2012/6/6': 'some other description'
};
$('#datepicker').datepicker({
beforeShowDay: function(date) {
var search = date.getFullYear() + '/' + (date.getMonth() + 1) + '/' + date.getDate();
if (search in dates) {
return [true, 'highlight', (dates[search] || '')];
}
return [false, '', ''];
}
});
});
不确定这是否仍然有用,但由于这对我有用,我想分享我所做的:
在我的JavaScript中:
var holidays= ["2016/09/18", "2016/09/19", "2016/01/01", "2016/05/01", "2016/06/27", "2016/08/15"];
$("#SomeID").datepicker({ beforeShowDay: highLight });
function highLight(date) {
for (var i = 0; i < holidays.length; i++) {
if (new Date(holidays[i]).toString() == date.toString()) {
return [true, 'ui-state-holiday'];
}
}
return [true];
}
.ui-state-holiday .ui-state-default {
color: red;
}
.ui-state-holiday .ui-state-default, .ui-datepicker-week-end .ui-state-default {
color: red;
}
这是结果:
(注意,我已将语言配置为西班牙语,但这对代码不重要)
虽然有点晚了,但这是我用来测试的JSFiddle:
https://jsfiddle.net/gq6kdoc9/
HTML:
<div id="datepicker"></div>
JavaScript:
var dates = ['11/13/2017', '11/14/2017'];
//tips are optional but good to have
var tips = ['some description', 'some other description'];
$('#datepicker').datepicker({
dateFormat: 'dd/mm/yy',
beforeShowDay: highlightDays,
showOtherMonths: true,
numberOfMonths: 3,
});
function highlightDays(date) {
for (var i = 0; i < dates.length; i++) {
if (new Date(dates[i]).toString() == date.toString()) {
return [true, 'highlight', tips[i]];
}
}
return [true, ''];
}
td.highlight {
border: none !important;
padding: 1px 0 1px 1px !important;
background: none !important;
overflow: hidden;
}
td.highlight a {
background: #ad3f29 url(bg.png) 50% 50% repeat-x !important;
border: 1px #88a276 solid !important;
}
基于上面Mike的工作示例构建!
date
变量进行格式化(我从这里找到了相关信息)。在这里。function highlightDays(date) {
for (var i = 0; i < dates.length; i++) {
var d = date.getDate();
var m = date.getMonth();
m += 1; // JavaScript months are 0-11
var y = date.getFullYear();
var dateToCheck = (d + "/" + m + "/" + y);
if (dates[i] == dateToCheck) {
return [true, 'highlight', tips[i]];
}
}
return [true, ''];
}
dates
数组:var dates = ['22/1/2014', '23/1/2014'];