英文:
uncaught ReferenceError: tgl is not defined
问题
我不太清楚导致错误的原因,因为我对JavaScript不熟悉。我在标头中使用了js引用变量:
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
selectable: true,
selectHelper: true,
select: function date(start, end, alldays){
$("#BookingModal").modal('toggle');
var tgl=moment(start).format('DD-MM-YYYY')
}
});
calendar.render();
});
当我在HTML主体中使用变量时,它会抛出错误:
未捕获的ReferenceError: tgl未定义
<h1 class="modal-title fs-5" id="exampleModalLabel"> <script type="text/javascript">document.write(tgl)</script></h1>
请注意,您需要确保在引用变量之前定义它,否则会导致"未定义"错误。
英文:
I dont really know what is causing the error since I'm new to JavaScript. I've referenced the variable in the header with js:
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
selectable: true,
selectHelper: true,
select: function date(start, end, alldays){
$("#BookingModal").modal('toggle');
var tgl=moment(start).format('DD-MM-YYYY')
}
});
calendar.render();
});
And when I want to use the variable in the body of the html, it throws the error:
> uncaught ReferenceError: tgl is not defined
<h1 class="modal-title fs-5" id="exampleModalLabel"> <script type="text/javascript">document.write(tgl)</script></h1>
答案1
得分: 1
模态框标题应该根据所点击的日历单元格动态生成。
因此,您需要等待用户点击日历单元格,以了解在select
回调中提供的startDate
是什么。
下面的代码将在解析HTML时在页面加载时执行...
在DOMContentLoaded
触发后,将执行您的其余代码之前。
document.addEventListener("DOMContentLoaded", function () {
// 在全局范围内声明tgl变量
let tgl;
// 在模态框打开时,设置模态框标题
$("#BookingModal").on("show.bs.modal", function (event) {
$("#BookingModal").find(".modal-title").text(tgl);
});
// FulCalendar
var calendarEl = document.getElementById("calendar");
var calendar = new FullCalendar.Calendar(calendarEl, {
selectable: true,
select: function date(start, end, alldays) {
// 在此处设置tgl
tgl = moment(start.startStr).format("DD-MM-YYYY");
// 打开模态框
$("#BookingModal").modal("show");
}
});
calendar.render();
});
请注意,我使用了start.startStr
传递给moment.js。您可以在FullCalendar中控制台记录start
对象,以了解原因。
英文:
The modal title you whish to have is dynamic based on the calendar cell that was clicked.
So you have to wait for the user to click on a calendar cell to know what is the startDate
provided in this select
callback.
The code below will be exectuted on page load while parsing the HTML...
Long before the rest of your code that will execute after DOMContentLoaded
has fired.
<script type="text/javascript">document.write(tgl)</script>
The events are firing in this order and that is how tgl
should be used in each of them:
DOMContentLoaded
--tgl
is declared- FullCalendar's
select
--tgl
value is setted show.bs.modal
--tgl
value is used
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
document.addEventListener("DOMContentLoaded", function () {
// Have the tgl variable at global scope
let tgl;
// On modal open, set the modal title
$("#BookingModal").on("show.bs.modal", function (event) {
$("#BookingModal").find(".modal-title").text(tgl);
});
// FulCalendar
var calendarEl = document.getElementById("calendar");
var calendar = new FullCalendar.Calendar(calendarEl, {
selectable: true,
select: function date(start, end, alldays) {
// Set tgl here
tgl = moment(start.startStr).format("DD-MM-YYYY");
// Open modal
$("#BookingModal").modal("show");
}
});
calendar.render();
});
<!-- language: lang-html -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/fullcalendar@6.0.2/index.global.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>
<div id="calendar"></div>
<div class="modal fade" id="BookingModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">This title will be replaced</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
Some modal body here...
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Ok</button>
</div>
</div>
</div>
</div>
<!-- end snippet -->
Also, notice I used start.startStr
to pass to moment.js. You can console log that start
object of FullCalendar to understand why.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论