gpt4 book ai didi

javascript - 为什么我在 div JavaScript 中得到额外的正斜杠

转载 作者:行者123 更新时间:2023-12-03 09:18:47 25 4
gpt4 key购买 nike

出于某种原因,我在动态填充的日历的标题后面的行上呈现了 8 个正斜杠。其基本代码如下:

<script type="text/javascript">
function getCalendar() {
var now = new Date();
var thisMonth = now.getMonth();
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var month = months[now.getMonth()];
var thisYear = now.getFullYear();
var firstDay = new Date(thisYear, thisMonth, 1, 0, 0, 0, 0);
var firstDayOfWeek = firstDay.addDays(-firstDay.getDay()); // get the first date of the 5-week calendar
var calendar = '<header> <h1 class="month-year">' + month + '&nbsp;&nbsp;&nbsp;&nbsp;' + thisYear + '</h1> ';
calendar += '</header> <table> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> ';
var calMonth = new Array(6); // least amount that will contain an entire month every time
calMonth[0] = new Array(7); // each week contains 7 days
calMonth[1] = new Array(7);
calMonth[2] = new Array(7);
calMonth[3] = new Array(7);
calMonth[4] = new Array(7);
calMonth[5] = new Array(7);
for(var i = 0; i < 6; i++) {
for(var j = 0; j < 7; j++) {
calMonth[i][j] = new Date(firstDayOfWeek); // input each date for the calendar into the array
firstDayOfWeek = new Date(firstDayOfWeek.addDays(1));
}
}
for(var i = 0; i < 6; i++) {
calendar += '<tr>';
for(var j = 0; j < 7; j++) {
var currDate = new Date(calMonth[i][j]);
day = currDate.getDate();
if(day == now.getDate() && currDate.getMonth() == now.getMonth()) {
calendar += '<td class="moderate current-day">' + day + '</td>';
} else if(currDate < now) {
calendar += '<td class="event heavy"><s>' + day + '</s></td>';
} else if(currDate.getDay() >= 5) {
calendar += '<td class="light">' + day + '</td>/';
} else if(currDate.getMonth() > now.getMonth() && currDate.getYear() == now.getYear()
|| currDate.getMonth() < now.getMonth() && currDate.getYear() < now.getYear()) {
calendar += '<td class="event moderate next-month">' + day + '</td>';
} else if(currDate.getMonth() < now.getMonth() && currDate.getYear() == now.getYear()
|| currDate.getMonth() > now.getMonth() && currDate.getYear() > now.getYear()) {
calendar += '<td class="event heavy prev-month">' + day + '</td>';
} else { calendar += '<td class="event light">' + day + '</td>'; }
}
calendar += '</tr>';
}
document.getElementById('calendar').innerHTML = calendar;
}

Date.prototype.addDays = function (n) {
var time = this.getTime();
var changedDate = new Date(time + (n * 24 * 60 * 60 * 1000));
this.setTime(changedDate.getTime());
return this;
}
</script>

当它渲染到<div class='calendar' id='calendar'></div>时,由于某种原因它填充 ////////在标题正下方的行上,当前呈现 August 2015 .

表头和正文之间没有任何内容,并且在渲染日历之前(在 onload 事件期间),实际的 div 中也没有任何内容。其图像如下所示:

enter image description here

此外,可以在this website查看具有当前样式的正在进行的版本。 .

谁能帮我解开这个谜团?

谢谢

-C§

最佳答案

标签后面的 for 循环内有一个杂散的正斜杠。下面将其删除。

<script type="text/javascript">
function getCalendar() {
var now = new Date();
var thisMonth = now.getMonth();
var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
var month = months[now.getMonth()];
var thisYear = now.getFullYear();
var firstDay = new Date(thisYear, thisMonth, 1, 0, 0, 0, 0);
var firstDayOfWeek = firstDay.addDays(-firstDay.getDay()); // get the first date of the 5-week calendar
var calendar = '<header> <h1 class="month-year">' + month + '&nbsp;&nbsp;&nbsp;&nbsp;' + thisYear + '</h1> ';
calendar += '</header> <table> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> ';
var calMonth = new Array(6); // least amount that will contain an entire month every time
calMonth[0] = new Array(7); // each week contains 7 days
calMonth[1] = new Array(7);
calMonth[2] = new Array(7);
calMonth[3] = new Array(7);
calMonth[4] = new Array(7);
calMonth[5] = new Array(7);
for(var i = 0; i < 6; i++) {
for(var j = 0; j < 7; j++) {
calMonth[i][j] = new Date(firstDayOfWeek); // input each date for the calendar into the array
firstDayOfWeek = new Date(firstDayOfWeek.addDays(1));
}
}
for(var i = 0; i < 6; i++) {
calendar += '<tr>';
for(var j = 0; j < 7; j++) {
var currDate = new Date(calMonth[i][j]);
day = currDate.getDate();
if(day == now.getDate() && currDate.getMonth() == now.getMonth()) {
calendar += '<td class="moderate current-day">' + day + '</td>';
} else if(currDate < now) {
calendar += '<td class="event heavy"><s>' + day + '</s></td>';
} else if(currDate.getDay() >= 5) {
calendar += '<td class="light">' + day + '</td>';
} else if(currDate.getMonth() > now.getMonth() && currDate.getYear() == now.getYear()
|| currDate.getMonth() < now.getMonth() && currDate.getYear() < now.getYear()) {
calendar += '<td class="event moderate next-month">' + day + '</td>';
} else if(currDate.getMonth() < now.getMonth() && currDate.getYear() == now.getYear()
|| currDate.getMonth() > now.getMonth() && currDate.getYear() > now.getYear()) {
calendar += '<td class="event heavy prev-month">' + day + '</td>';
} else { calendar += '<td class="event light">' + day + '</td>'; }
}
calendar += '</tr>';
}
document.getElementById('calendar').innerHTML = calendar;
}

Date.prototype.addDays = function (n) {
var time = this.getTime();
var changedDate = new Date(time + (n * 24 * 60 * 60 * 1000));
this.setTime(changedDate.getTime());
return this;
}
</script>

关于javascript - 为什么我在 div JavaScript 中得到额外的正斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31900817/

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