javascript - 从一年中的几天开始用 HTML 填充 Bootstrap 表

标签 javascript bootstrap-4

我试图用一年中的天数填充 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/

相关文章:

javascript - WebGL 在页面上崩溃时的事件?

javascript - 错误 : [$injector:modulerr] with angular JS [GULP CONTEXT]

javascript - 如何在jquery选择更改事件上遍历dom树

reactjs - 如何获取 Reactstrap 的 CustomInput Switch 组件的状态,以及如何从数组映射开关?

javascript - 为什么 select2 不起作用?

javascript - IE9 JavaScript 错误 : SCRIPT5007: Unable to get value of the property 'ui' : object is null or undefined

html - 样式 Bootstrap 4 自定义复选框

html - 为什么我的引导下拉菜单占据了网站的整个宽度,即使它很小

css - Bootstrap 4水平滚动div

html - Bootstrap 4 导航栏品牌 - 是否可以在较小的断点处使用不同的 Logo ?