简体   繁体   English

使用 jQuery 实现的完整日历

[英]Full Calendar implemented using jQuery

I'm trying to implement FullCalendar using jQuery我正在尝试使用 jQuery 实现 FullCalendar

$(document).ready(function() {
  document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = $('#calendar');
    var calendar = new FullCalendar.Calendar(calendarEl, {
      initialView: 'dayGridMonth'
    });
    calendar.render();
  });
});

but the calendar doesn't show on the browser但日历没有显示在浏览器上

-for more information about the FullCalendar:[FullCalendar][1]- [1]: https://fullcalendar.io/ -有关 FullCalendar 的更多信息:[FullCalendar][1]- [1]:https://fullcalendar.io/

FullCalendar accepts an Element object into its constructor. FullCalendar 将元素 object 接受到其构造函数中。 It does not accept a jQuery object.它不接受 jQuery object。 You cannot use a jQuery object in the way you've shown in your code.您不能按照您在代码中显示的方式使用 jQuery object。 jQuery doesn't give you any advantages in this situation. jQuery 在这种情况下不会给您任何优势。 (This does not prevent you from using jQuery elsewhere in your page, you just can't use a jQuery object to initialise the calendar.) (这并不妨碍您在页面的其他地方使用 jQuery,只是不能使用 jQuery object 来初始化日历。)

Ways to get an Element object:获取元素 object 的方法:

  1. Use document.getElementById as shown in the fullCalendar introductory guide - eg document.getElementById("calendar");使用fullCalendar 介绍指南中所示的document.getElementById - 例如document.getElementById("calendar");

  2. use document.querySelector - eg document.querySelector('#calendar')使用document.querySelector - 例如document.querySelector('#calendar')

  3. If, for some reason, you really insist on having the unnecessary overhead of using a jQuery constructor and object, then you can create a jQuery object using $("#calendar") and then get the first matched Element object out of it by using $("#calendar")[0] , and pass that to fullCalendar. If, for some reason, you really insist on having the unnecessary overhead of using a jQuery constructor and object, then you can create a jQuery object using $("#calendar") and then get the first matched Element object out of it by using $("#calendar")[0] ,并将其传递给 fullCalendar。 But this is inefficient and unnecessary.但这是低效且不必要的。 jQuery is just getting in your way if you do this, without adding any value.如果您这样做,jQuery 只会妨碍您,而不会增加任何价值。

Here's a simple example of initialising the calendar using document.querySelector :这是一个使用document.querySelector初始化日历的简单示例:

document.addEventListener('DOMContentLoaded', function() {
  var calendarEl = document.querySelector('#calendar');

  var calendar = new FullCalendar.Calendar(calendarEl, {
  });

  calendar.render();
});

Live demo: https://codepen.io/ADyson82/pen/bGgEPPK现场演示: https://codepen.io/ADyson82/pen/bGgEPPK


PS If you then wanted to use jQuery to do more things to the calendar element, after the calendar.render() line you could easily wrap the calendarEl in a jQuery object so you can then use jQuery functions with it for other purposes. PS If you then wanted to use jQuery to do more things to the calendar element, after the calendar.render() line you could easily wrap the calendarEl in a jQuery object so you can then use jQuery functions with it for other purposes. For example:例如:

var calendarEl = document.querySelector('#calendar');

var calendar = new FullCalendar.Calendar(calendarEl, {
});

calendar.render();

var calendarjQ = $(calendarEl); //wrap calendarEl, which is an Element, in a jQuery object
// then do whatever you want with calendarjQ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM