问题描述
我正在使用全日历进行日间活动预订.我想根据给定的日期进行某些日间颜色更改,但是它会在整个日历中呈现颜色,从而使所有单元格变为绿色.这是代码,
I am using fullcalendar for day event booking.I want to make some day color change based on given dates.But it render the color in the whole calendar.It makes all the cell green.Here is the code,
dayRender: function (date, cell) {
var disabledDates = ["2017-08-17","2017-08-23"];
if ($.inArray(date.format("YYYY-MM-DD"),disabledDates)) {
//alert("success");
cell.css("background-color", "green");
}
}
推荐答案
您没有正确使用 $.inArray
.它不会返回用于解析 if
语句的 true
或 false
.而是返回一个整数,该整数表示它首先找到匹配值的索引.如果找不到匹配项,则返回-1.
You are not using $.inArray
correctly. It does not return true
or false
, which you would use to resolve an if
statement. Instead it returns an integer representing the index at which it first finds a matching value. If it doesn't find a match, it returns -1.
您的问题正在发生,因为JavaScript将 0
大致等同于 false
(即 0 == false
,但 0!)!== false
),其他所有数字则大致等于 true
.因此,如果 $.inArray
返回除 0
以外的任何内容(即,在与第一个数组索引中的值匹配的位置返回0),则您的 if
语句将其视为已返回 true
,并因此更改了背景色.这就是为什么它会更改所有变量的原因(2017年8月17日除外).
Your problem is occurring because JavaScript treats 0
as loosely equal to false
(i.e. 0 == false
, but 0 !== false
), and all other numbers loosely as equal to true
. Therefore if $.inArray
returns anything other than 0
(i.e. 0 is returned where matched the value in the first array index), your if
statement will treat is as if it had returned true
, and consequently change the background colour. That's why it changes it for all of them except 2017-08-17.
此版本将正常运行,在设置背景色之前测试索引返回> -1(整数,不是布尔值):
This version will work correctly, testing that the index return is > -1 (an integer, not a bool) before setting the background colour:
dayRender: function(date, cell) {
var disabledDates = ["2017-08-17", "2017-08-23"];
if ($.inArray(date.format("YYYY-MM-DD"), disabledDates) > -1) {
console.log(date.format("YYYY-MM-DD"));
cell.css("background-color", "green");
}
}
请参阅 https://api.jquery.com/jQuery.inArray/更详细的解释.
这篇关于在给定的日期,dayRender无法在全日历中正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!