【问题标题】:Call fullcalendar function from another file从另一个文件调用 fullcalendar 函数
【发布时间】:2021-09-04 08:34:47
【问题描述】:

我正在使用全日历 V.5,我需要从另一个页面调用全日历功能。但是遇到 calendar.______ 的错误不是函数 我已经检查了这个 Calling a fullcalendar method/callback from another file ,但不适用于版本 5。

按侧边栏切换按钮,查看错误

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

    var calendar = new FullCalendar.Calendar(calendarEl, {
      timeZone: 'UTC',
      initialView: 'timeGridWeek',
      headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'timeGridWeek,timeGridDay'
      },
      events: 'https://fullcalendar.io/demo-events.json'
    });

    calendar.render();
   
  });
   $('.icon').click(function(){
      $('.sidebar').toggleClass('in');
      calendar.render();
    });
.sidebar{
  background:#eeeeee;
  height:100vh;
  width:200px;  
}
.h-100{
  height:100vh!important;
}
.main{
  flex:1;
  padding:20px
}
.icon{
  cursor: pointer;
}
.in{
  width:25px!important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/fullcalendar-scheduler@5.7.0/main.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/fullcalendar-scheduler@5.7.0/main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="d-flex h-100">
  <div class="sidebar in">
    <div class="icon text-right px-2">
      <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-left" viewBox="0 0 16 16">
  <path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
</svg>
    </div>
    
  </div>
  <div class="main">
     <div id='calendar'></div>
  </div>
</div>

【问题讨论】:

  • 您是否尝试过在全局范围内声明 calendar 变量?尝试在 addEventListener 上方声明它并检查。
  • 你能解释一下如何在外面声明吗?我不完全理解。@dee

标签: javascript html jquery fullcalendar-5


【解决方案1】:

日历变量不在可能遇到错误的$(".icon") 函数的范围内。试试这个。

let calendar;
document.addEventListener('DOMContentLoaded', function() {
    var calendarEl = document.getElementById('calendar');

    calendar = new FullCalendar.Calendar(calendarEl, {
      timeZone: 'UTC',
      initialView: 'timeGridWeek',
      headerToolbar: {
        left: 'prev,next today',
        center: 'title',
        right: 'timeGridWeek,timeGridDay'
      },
      events: 'https://fullcalendar.io/demo-events.json'
    });

    calendar.render();
   
  });
   $('.icon').click(function(){
      $('.sidebar').toggleClass('in');
      calendar.render();
    });
.sidebar{
  background:#eeeeee;
  height:100vh;
  width:200px;  
}
.h-100{
  height:100vh!important;
}
.main{
  flex:1;
  padding:20px
}
.icon{
  cursor: pointer;
}
.in{
  width:25px!important;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<link href="https://cdn.jsdelivr.net/npm/fullcalendar-scheduler@5.7.0/main.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/npm/fullcalendar-scheduler@5.7.0/main.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="d-flex h-100">
  <div class="sidebar in">
    <div class="icon text-right px-2">
      <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-text-left" viewBox="0 0 16 16">
  <path fill-rule="evenodd" d="M2 12.5a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h7a.5.5 0 0 1 0 1h-7a.5.5 0 0 1-.5-.5zm0-3a.5.5 0 0 1 .5-.5h11a.5.5 0 0 1 0 1h-11a.5.5 0 0 1-.5-.5z"/>
</svg>
    </div>
    
  </div>
  <div class="main">
     <div id='calendar'></div>
  </div>
</div>

【讨论】:

  • 更好的解决方案是将$('.icon').click(function() 放在 DOMContentLoaded 块内 - 为了安全起见,无论如何它都应该在那里,以确保页面在尝试将事件侦听器附加到其中的对象之前已完全加载
  • @ADyson 。我很早就提到了 $('.icon').click(function() 与 DOMContentLoaded 块不在同一页面中,将其放入内部是不可能的,因为它们都是不同的页面
  • @Amalnandan 你从来没有特别提到过。 “另一页”有些模糊,但实际上并不清楚这意味着什么。您需要编辑问题并澄清场景......“其他页面”是否通过 AJAX 加载到您正在使用的页面中,也许?还是两个页面都在浏览器中的单独选项卡中打开?还是一页接一页地加载?所有这些都是不同的场景,具有不同的可能问题和解决方案。请更新您的帖子,并告诉我们您想要控制的确切情况。
猜你喜欢
  • 2013-12-17
  • 2019-08-11
  • 2021-08-30
  • 2020-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多