簡體   English   中英

使用jQuery在js中的日歷上創建事件,我陷入了一個奇怪的錯誤

[英]Creating events on a calendar in js with jQuery, i'm stuck with a strange bug

我在代碼中遇到了一個奇怪的錯誤。 實際上,我修改了不清楚的舊代碼,最終遇到了這個問題。 您可以在這里擺弄一個例子。

基本上,我有一個日歷,我需要延長多日活動的時間,以便它們跨越正確的天數。 一切正常,但有一種情況:如果每月的第一天是一周的第一天。 在這種情況下,跨越多天的事件只會在第一周縮短一天。 我通過使用解決方法解決了這個問題

// There is a bug that happens only when the first day of the 
// month is the first day of the week. In that case events that start on the first of the month
// or multy day events that span from the previous month end up being a day too short in the 
// first week. So for example an event that lasted the full month was only six days in the first week.
// This workaround works for me, couldn't understand what's wrong.
// if (startDay === 1 && daysFirstWeek === 7) {
   // days += 1;
// }

如果您取消注釋,則一切正常,但是顯然這不是解決方案。

我需要在這件事上有新的眼光,這可能是整個概念的底下錯了,我應該從頭開始,但是當我找不到解決方案時,我很生氣。

更新

好,這次知道了 在其他情況下,您的支票有誤:

        if (cellNum !== 0) {
            // Extend initial event bar to the end of first (!) week.
            if (curLine === 0) {
                days++;
            }
        } else if (day > startDay && daysLeft !== 0) {    // WRONG CHECK HERE!

day> startDay必須是day> = startDay。 然后所有工作正常。

這是因為循環初始檢查:

if (day >= startDay && day <= endDay) {

由於后面的檢查是day> startDay,因此錯過了添加第幾天出現的第一個單元格,並且curLine不會按時遞增。 下一行的第一個單元格最終以curLine比其應少的1進行處理。 如果您在控制台上記錄curLine和cellNum,您將看到它們像這樣運行,並且不會在應有的位置對齊。 修復后,處理將像第一個單元格那樣進入內部條件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM