所以我的页面中有一个 fullCalendar (v.4.2.0) 实例(在 document.ready 上加载/构建),我想在其他函数中访问该 fullCalendar 实例以动态添加事件。
使用 $('#calendar').fullCalendar('getView')
显示 fullCalendar() 方法不存在的错误:
$(...).fullCalendar is not a function
如何访问该实例?
我用来生成日历的代码如下:
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'timeGrid', 'dayGrid' ],
defaultView: 'timeGridWeek',
height: 700,
locale: 'pt',
allDaySlot: false,
handleWindowResize: true,
events: [
{
title: 'Teste1', // a property!
start: '2019-07-23 16:00',
end: '2019-07-23 17:00',
backgroundColor: '#fcba03',
borderColor: '#fcba03',
}
]
});
calendar.render();
最佳答案
由于您使用的是 fullCalendar 版本 4,它不再作为 jQuery 插件编写,因此无法使用 jQuery 语法来访问这些方法。相反,只需引用您的 calendar
变量(它可能需要在您的应用程序中具有广泛的范围 - 全局或通过其他方式轻松访问)。您会注意到,您的现有代码在调用 calendar.render();
时已经完全做到了这一点。
此外,在“getView”方法的特定情况下,它已被简单地替换为引用当前 View 的 view
属性。
例如(见最后一行):
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
plugins: [ 'timeGrid', 'dayGrid' ],
defaultView: 'timeGridWeek',
height: 700,
locale: 'pt',
allDaySlot: false,
handleWindowResize: true,
events: [
{
title: 'Teste1', // a property!
start: '2019-07-23 16:00',
end: '2019-07-23 17:00',
backgroundColor: '#fcba03',
borderColor: '#fcba03',
}
]
});
calendar.render();
var vw = calendar.view; //get the current view
引用文档
关于javascript - 我如何获得 fullcalendar v.4.2.0 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57178187/