3

对不起这个最有可能简单的问题。我想JSON向 fullCalendar 添加一些自定义数据并在我的 dayClick 回调函数中使用它。更具体地说,我想在 dayClick 中访问和更改 customData 上的元素。但是我不知道该怎么做。是否可以不更改 fullcalendar 源代码?

提前非常感谢 Jens

$('#calendar').fullCalendar({
    ....
    firstDay: 1,
    customData: { foo: 'bar', more: 'baz' },
    dayClick: function(date, allDay, jsEvent, view) {
        // Within the callback function, this is set to the <td> of the clicked day.
        var t = $(this);
        var foo = customData.foo; // does not work
        customData.more = 'foo'; // does not work

        // Change the day's background color
        if (t.hasClass('badge-important') && foo == 'bar') {
            t.removeClass('badge-important');               
        } else {
            t.addClass('badge-important');              
        }
    },
});
4

1 回答 1

1

问题是您没有在任何地方定义 customData 变量。以下创建变量来存储 JSON 的方法将解决您的问题。检查下面的代码:

var customData = {"foo":"bar"};

$('#calendar').fullCalendar({
....
firstDay: 1,
customData: customData,
dayClick: function(date, allDay, jsEvent, view) {
    // Within the callback function, this is set to the <td> of the clicked day.
    var t = $(this);
    var foo = customData.foo; // will now work
    customData.more = 'foo' // will work too

    // Change the day's background color
    if (t.hasClass('badge-important') && foo == 'bar') {
        t.removeClass('badge-important');               
    } else {
        t.addClass('badge-important');              
    }
},
});

我希望它有所帮助。干杯

于 2012-11-14T15:40:40.200 回答