我试图用一年中的天数填充 Bootsrap 表。目标是生成一个带有垂直 Assets 的水平日历 View ,该项目处于诞生阶段......我是编码新手,所以请友好......
我怎样才能填充表格?
我收到错误:days.forEach 不是函数
任何帮助或方向将不胜感激!
function daysOfAYear(year)
{
return isLeapYear(year) ? 366 : 365;
}
function isLeapYear(year) {
return year % 400 === 0 || (year % 100 !== 0 && year % 4 === 0);
}
const days = daysOfAYear(2020);
// populate das in table...
function showDays(days) {
let output = '';
days.forEach(day => {
output += `
<th scope="col">${day}</th>
`;
});
// Output days
document.getElementById('date-calendar').insertBefore(output)
};
console.log(showDays(days));
这是我的 HTML 的一部分: <div class="table-responsive">
<table class="table table-striped table-sm">
<thead>
<tr>
<th colspan="6" class="text-center">January</th>
</tr>
</thead>
<thead>
<tr>
<th>Name</th>
<th>01.01</th>
<th>02.01</th>
<th>03.01</th>
<th>04.01</th>
</tr>
</thead>
<tbody class="date-calendar">
<tr>
<td>John Doe</td>
<td>Lorem</td>
<td>ipsum</td>
<td>dolor</td>
<td>sit</td>
</tr>
</tbody>
</table>
</div>
最佳答案
forEach
方法,遍历一个数组,你的值 days
在一个整数中。
要解决此问题,您需要使用 for
循环,这里有一些文档:https://www.w3schools.com/js/js_loop_for.asp
在您的情况下, for 循环将是
for (let day = 1; day <= days; day++) {
output += `
<th scope="col">${day}</th>
`;
}
关于javascript - 从一年中的几天开始用 HTML 填充 Bootstrap 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64563686/