js for循环 制作空课表
时间: 2023-08-11 22:21:56 浏览: 45
可以使用以下代码来使用 for 循环制作空课表:
```html
<table>
<thead>
<tr>
<th>时间</th>
<th>星期一</th>
<th>星期二</th>
<th>星期三</th>
<th>星期四</th>
<th>星期五</th>
</tr>
</thead>
<tbody>
<script>
const rows = 7; // 行数
const cols = 6; // 列数
for (let i = 0; i < rows; i++) {
document.write('<tr>');
for (let j = 0; j < cols; j++) {
if (j === 0) {
document.write(`<td>${i + 1}</td>`); // 输出行号
} else {
document.write('<td></td>'); // 输出空单元格
}
}
document.write('</tr>');
}
</script>
</tbody>
</table>
```
上面的代码会生成一个包含时间和星期的空课表,可以根据需要修改行数和列数。在 for 循环中,使用嵌套的循环来输出表格的每一行和每一个单元格,通过判断列数来输出行号或空单元格。空单元格使用空字符串输出,可以根据需要修改为其他占位符。