我想在日历上显示,一年中哪些日期是空闲日期。对于这些,我想设置一个红色背景。
我的问题是,使用这段代码,它会为所有日期提供红色背景。
我在 dayRender 事件中使用它。
var unnep_napok =
[
"2019-01-12",
"2019-01-15"
];
$('#calendar').fullCalendar({
events: valami,
lang: 'hu',
dayClick: function(event) {
$(cell).removeClass('ui-widget-content');
$(cell).addClass('holiday');
$(this).css('background-color', 'green');
},
defaultView: 'month',
contentHeight: 'auto',
slotEventOverlap: false,
eventRender: function(eventObj, $el) {
$el.popover({
title: ' ',
content: eventObj.description,
trigger: 'hover',
placement: 'top',
container: 'body'
});
},
dayRender: function (date, cell) {
for(i = 0; i < unnep_napok.length; i++ )
{
cell.css("background-color", "red");
}
}
});
比较更新:
dayRender: function (date, cell) {
for(i = 0; i < unnep_napok.length; i++ )
{
if(date == unnep_napok[i] )
{
cell.css("background-color", "red");
}
}
}
Update 2, formatting array elements:
dayRender: function (date, cell)
{ for(i = 0; i < unnep_napok.length; i++ ) { var datum = unnep_napok[i].moment.format('yyyy-mm-dd'); if(date.getDate() == datum ) { cell.css("background-color", "red"); } } }
最佳答案
在你的更新之后,仍然有一些问题可以通过更仔细地阅读文档(和我之前的评论)来解决:
1) 我没有给你在“格式”命令中使用的字面值。你读过documentation吗?完全?如您所见,正确的格式是 YYYY-MM-DD
(大字母而不是小字母)。
2) unnep_napok[i].moment.format
...这不是你的方式create a momentJS object .我希望您的浏览器在控制台中给出有关此的错误。
3) 但无论如何 2) 并不重要,因为正如我在上一条评论中提到的,您需要格式化的是 date
值……您的 unnep_napok 值已经是字符串了!!
4) date.getDate()
.. 我不知道你从哪里得到这个?? MomentJS 没有记录任何此类功能。
这应该适合你:
dayRender: function (date, cell)
{
for(i = 0; i < unnep_napok.length; i++ )
{
if(date.format('YYYY-MM-DD') == unnep_napok[i])
{
cell.css("background-color", "red");
}
}
}
关于javascript - 在完整日历中突出显示自定义日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54147143/