我需要突出显示开始日期和结束日期之间的日期,我应该能够指定。谁能帮我?
7 回答
您可以使用beforeShowDay 事件。它将为需要在日历中显示的每个日期调用。它传入一个日期并返回一个数组[0]= isSelectable, [1]= cssClass, [2]=Some tooltip text
$('#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从这里制作一个带有核心、小部件和日期选择器的包。
要放在前面的 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 的小图像才能使其正常工作
以为我会投入两分钱,因为它看起来比其他人更快更轻:
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];
}
在我添加的 jquery-ui-theme.css
.ui-state-holiday .ui-state-default {
color: red;
}
如果您还想突出显示周末,则必须改用此 CSS
.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, ''];
}
和 CSS:
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;
}
基于上面迈克的工作示例!
mugur,您的代码在 Firefox 或 Safari 中不太适合我,它需要对date
var 进行格式化(我从这里得到)。这里
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'];