FullCalander 5 添加事件

我正在构建一个 CRM 并尝试在日历 div 之外制作一个按钮,如果我按下按钮,它应该在上创建一个事件并且它不起作用。有人可以指出我做错了什么吗?


这是我试过的:



  $(document).on("click", "#testbtn", function(){

    var calendarEl = $("#calendar");

    calendarEl.addEvent({events: [

      {

        title: 'Second Event',

        start: '2020-08-08T12:30:00',

        end: '2020-08-08T13:30:00'

      }

    ]});

  });

这是我的整个脚本:


<html>

<body>

<script src='../lib/main.js'></script>

<script>


  document.addEventListener('DOMContentLoaded', function() {

    var calendarEl = document.getElementById('calendar');


    var calendar = new FullCalendar.Calendar(calendarEl, {

    <!-- all the options here -->

    calendar.render();

  });


</script>

</head>

<body>

<button id="testbtn">Add lunch time</button>

  <div id='calendar-container'>

    <div id='calendar'></div>

  </div>

<script src="/assets/plugins/jquery/jquery.min.js"></script>

<script>

  var options = {  /*Add options here*/};

  var calendarEl = $("#calendar");

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


  $(document).on("click", "#testbtn", function(){

    calendar.addEvent({

      title: 'Lunch',

      start: '2020-08-08T12:30:00',

      end: '2020-08-08T13:30:00'

    });

    calendar.render();

  });

</script>


泛舟湖上清波郎朗
浏览 128回答 2
2回答

白板的微信

有两个问题:calendarEl是一个 DOM 元素。它没有.addEvent属性(至少没有 FullCalendar 属性。)的参数addEvent是单个Event 对象,而不是具有键events和事件数组作为值的对象。该addEvent演示可能对您有所帮助。您需要将使用关键字FullCalendar.Calendar创建的实例传递给事件处理函数。new&nbsp; var calendarEl = $("#calendar").get(0);&nbsp; var calendar = new FullCalendar.Calendar(calendarEl, {&nbsp; &nbsp; initialView: 'dayGridMonth'&nbsp; });&nbsp;&nbsp; calendar.render();&nbsp; $("#testbtn").on("click", function(){&nbsp; &nbsp; calendar.addEvent({&nbsp; &nbsp; &nbsp; &nbsp; title: 'Second Event',&nbsp; &nbsp; &nbsp; &nbsp; start: '2020-08-08T12:30:00',&nbsp; &nbsp; &nbsp; &nbsp; end: '2020-08-08T13:30:00'&nbsp; &nbsp; &nbsp; });&nbsp; });

明月笑刀无情

看起来您需要在加载 jquery 脚本后发布此信息。这已经加载了事件。我们只是添加一个事件。身体<body><button id="testbtn">Add lunch time</button>&nbsp; <div id='calendar-container'>&nbsp; &nbsp; <div id='calendar'></div>&nbsp; </div><script src="/js/jquery/jquery.min.js"></script></body>然后调用全日历插件<script>&nbsp; var calendarEl = $("#calendar").get(0);&nbsp; var calendar = new FullCalendar.Calendar(calendarEl, {&nbsp; &nbsp; /*options...*/&nbsp; &nbsp; events:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'First Event',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start: '2020-08-08T10:30:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end: '2020-08-08T11:30:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extendedProps: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: 'Test 1'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: '#336e03'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: 2,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: 'Second Event',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start: '2020-08-08T15:30:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end: '2020-08-08T16:30:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; extendedProps: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description: 'Test 2'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; });&nbsp; calendar.render();/*this is the action when the button is pressed*/&nbsp; $("#testbtn").on("click", function(){&nbsp; &nbsp; calendar.addEvent({&nbsp; &nbsp; &nbsp; title: 'Third Event',&nbsp; &nbsp; &nbsp; start: '2020-08-08T12:30:00',&nbsp; &nbsp; &nbsp; end: '2020-08-08T13:30:00'&nbsp; &nbsp; });&nbsp; });</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript