javascript - 尝试用js创建日历表

标签 javascript jquery html

我的任务是显示日历 I js。所以得到了该月的第一天,并尝试使用下面代码中描述的逻辑制作一个表格。由于某种原因,下面的代码除了表头之外不打印任何内容。

var year = parseFloat(prompt("Enter Year: "))
var month = parseFloat(prompt("Enter Month in number: "))
var firstDay = (new Date(year, month)).getDay();
showcalander(firstDay);

function showcalander(day) {
  tbl = document.getElementById("calendar-body").innerHTML = "";
  let date = 1;
  for (i = 1; i <= 5; i++) {
    let row = document.createElement("tr");
    for (j = 1; j <= 7; j++) {
      if (i == 1 && j < day) {
        cell = document.createElement("td");
        cellText = document.createTextNode("");
        cell.appendChild(cellText);
        row.appendChild(cell);
      } else if (date > 30) {
        break;
      } else {
        cell = document.createElement("td");
        cellText = document.createTextNode(date);
        cell.appendChild(cellText);
        row.appendChild(cell);
        date++;
      }

    }
    tbl.appendChild(row);
  }
}
<table id="calendar">
  <thead>
    <tr>
      <th>Sun</th>
      <th>Mon</th>
      <th>Tue</th>
      <th>Wed</th>
      <th>Thu</th>
      <th>Fri</th>
      <th>Sat</th>
    </tr>
  </thead>
  <tbody id="calendar-body"></tbody>
</table>

最佳答案

问题出在这一行:

tbl = document.getElementById("calendar-body").innerHTML = ""

应该是:

tbl = document.getElementById("calendar-body")

var year = parseFloat(prompt("Enter Year: "))
var month = parseFloat(prompt("Enter Month in number: "))
var firstDay = (new Date(year, month)).getDay();
showcalander(firstDay);

function showcalander(day) {
  tbl = document.getElementById("calendar-body");
  let date = 1;
  for (i = 1; i <= 5; i++) {
    let row = document.createElement("tr");
    for (j = 1; j <= 7; j++) {
      if (i == 1 && j < day) {
        cell = document.createElement("td");
        cellText = document.createTextNode("");
        cell.appendChild(cellText);
        row.appendChild(cell);
      } else if (date > 30) {
        break;
      } else {
        cell = document.createElement("td");
        cellText = document.createTextNode(date);
        cell.appendChild(cellText);
        row.appendChild(cell);
        date++;
      }

    }
    tbl.appendChild(row);
  }
}
<table id="calendar">
  <thead>
    <tr>
      <th>Sun</th>
      <th>Mon</th>
      <th>Tue</th>
      <th>Wed</th>
      <th>Thu</th>
      <th>Fri</th>
      <th>Sat</th>
    </tr>
  </thead>
  <tbody id="calendar-body"></tbody>
</table>

关于javascript - 尝试用js创建日历表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54322873/

相关文章:

javascript - 获取每个元素获取高度并将其应用为 margin-top

javascript - 使用 ajax 获取 JSON 数据时,我收到意外的 token : error

php - load() 函数添加的内容在 ajax 操作后消失

javascript - 在 transitionend 事件监听器中触发事件监听器?

javascript - 在从 Node js 中的 mysql 中删除数据值之前,我如何显示警报并确认

javascript - 删除 parent 的点击事件但 child 是可点击的

javascript - 在 HTML 中的 li 之后切换开关

html - 如何在列表中居中 <UL>

javascript - 使用javascript将python/flask变量注入(inject)HTML表格单元格

java - 如何在 Android WebView 中使 JavaScript-Java 通信同步?