要在Calendar.js中顯示節(jié)假日安排,您需要執(zhí)行以下步驟:
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/5.9.0/main.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/5.9.0/main.min.css" />
<div id="calendar"></div>
$(document).ready(function () {
var calendarEl = document.getElementById("calendar");
var calendar = new FullCalendar.Calendar(calendarEl, {
initialView: "dayGridMonth",
locale: "zh-cn", // 設(shè)置中文
events: [], // 這里我們將填充節(jié)假日數(shù)據(jù)
eventContent: function (arg) {
return {
html: arg.event.title,
};
},
});
// 獲取節(jié)假日數(shù)據(jù)(這里只是一個(gè)示例,您需要根據(jù)實(shí)際情況獲取節(jié)假日數(shù)據(jù))
var holidays = [
{ title: "元旦", date: "2023-01-01" },
{ title: "春節(jié)", date: "2023-01-22" },
// ... 更多節(jié)假日
];
// 將節(jié)假日數(shù)據(jù)添加到日歷事件
holidays.forEach(function (holiday) {
calendar.addEvent({
title: holiday.title,
start: holiday.date,
});
});
// 渲染日歷
calendar.render();
});
這個(gè)示例中,我們首先創(chuàng)建了一個(gè)FullCalendar實(shí)例,并設(shè)置了初始視圖、語(yǔ)言和事件數(shù)據(jù)。然后,我們定義了一個(gè)節(jié)假日數(shù)組,并將其添加到日歷事件。最后,我們渲染了日歷。
請(qǐng)注意,這個(gè)示例中的節(jié)假日數(shù)據(jù)是硬編碼的。在實(shí)際應(yīng)用中,您可能需要從服務(wù)器獲取節(jié)假日數(shù)據(jù),或者根據(jù)用戶的地區(qū)設(shè)置動(dòng)態(tài)加載節(jié)假日數(shù)據(jù)。