外部触发FullCalendar的’dayClick’方法?
我正在使用Arshaw的FullCalendar,我希望模拟用户在页面加载后单击一个单元格。 我正在尝试这样做,所以我可能会转到一个URL,例如/Calendar/AddEvent
,日历将自动打开一个带有我的添加事件表单的对话框。
采取以下设置:( 在这里小提琴 )
HTML
jQuery的
$('#calendar').fullCalendar({ firstDay: 1, selectable: true, dayClick: function (start, allDay, jsEvent, view) { alert('You clicked me!'); } });
默认情况下,今天的单元格具有.fc-today
类,我尝试通过执行以下操作来利用它:
$('.fc-today').trigger('click');
这似乎不起作用。 无论如何我可以做类似的事情
$('#calendar').fullCalendar('dayClick', date)
任何帮助表示赞赏。
我也在我的用例中遇到过这个问题,这是我在找到fullcalendar源代码后找到的对我有用的东西。 您需要手动创建mousedown
和mouseup
事件(技术上, mouseup
是可选的,但您可以使用它来重置td
的’clicked’状态),其中包含您想要的元素的X和Y位置的坐标“点击”。 同样重要的是which
参数需要设置为1
以便通过isPrimaryMouseButton
检查 。
考虑到这一点,代码看起来像这样(注意我更改了$('#click')
以使用mousedown
事件,因此它只运行一次):
$('#click').on('mousedown', function () { var today = $('td.fc-today'); var down = new $.Event("mousedown"); var up = new $.Event("mouseup"); down.which = up.which = 1; down.pageX = up.pageX = today.offset().left; down.pageY = up.pageY = today.offset().top; today.trigger(down); today.trigger(up); //this is optional if you just want the dayClick event, but running this allows you to reset the 'clicked' state of the });
这是一个工作小提琴: http : //jsfiddle.net/vyxte25r/
您可以使用fc-today
类在td
内的div
上使用jquery trigger
事件:
$('td.fc-today div').trigger('click');
修改凯文的答案对我有点帮助。 我需要通过键盘访问dayClick事件。 使用dayRender方法将事件侦听器添加到每个单元格允许我们轻松处理每天的dayClick事件。 这是我完成它的方式:
dayRender: function(date, cell) { // Allow keyboard to access and interact with calendar days $(cell).attr("tabindex", "0").on("keydown", function(e) { if (e.which === 32 || e.which === 13) { var day = $(this); var down = new $.Event("mousedown"); var up = new $.Event("mouseup"); var eventParams = { which: 1, pageX: day.offset().left, pageY: day.offset().top }; day.trigger($.extend(true, {}, down, eventParams)); day.trigger($.extend(true, {}, up, eventParams)); } }); }
相关文档: https : //fullcalendar.io/docs/display/dayRender/