gpt4 book ai didi

javascript - 如何在js全年日历中选择范围并添加事件

转载 作者:行者123 更新时间:2023-12-02 22:36:20 25 4
gpt4 key购买 nike

我想知道如何选择范围并在 js-year-calendar 中添加事件。我的代码如下所示。

现在我可以选择日期并在控制台上显示,但我想注册此选定的事件并希望将其显示在日历中。

如果您已经有一些方法,请告诉我。

此代码引用以下文档:

https://year-calendar.github.io/js-year-calendar/documentation

new Calendar('.calendar', {
style: 'background',
dataSource: [{
startDate: new Date(2019, 1, 4),
endDate: new Date(2019, 1, 15),
color: "yellow"
}, {
startDate: new Date(2019, 3, 5),
endDate: new Date(2019, 5, 15),
color: "yellow"
}],
enableRangeSelection: true
});

document.querySelector('.calendar').addEventListener('selectRange', function(e) {
console.log("Select the range: " + e.startDate + " - " + e.endDate);
});

enter image description here

最佳答案

类似于 this

// Create a dataSource variable  

let dataSource = [
{
startDate: new Date(2019, 1, 4),
endDate: new Date(2019, 1, 15),
color: "yellow"
}, {
startDate: new Date(2019, 3, 5),
endDate: new Date(2019, 5, 15),
color: "yellow"
}
]

// Create the calendar with the dataSource

let calendar = new Calendar('#calendar', {
style: 'background',
dataSource: dataSource,
enableRangeSelection: true
});


document.querySelector('#calendar').addEventListener('selectRange', function(e) {
// Append the new data to dataSource
dataSource.push({
startDate: e.startDate,
endDate: e.endDate,
color: "red"
})

// Set the updated dataSource as main source
calendar.setDataSource(dataSource)
});

关于javascript - 如何在js全年日历中选择范围并添加事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58727808/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com