我有以下html表:
<table>
<tr>
<th> </th>
<th> Monday </th>
<th> Tuesday </th>
<th> Wednesday</th>
<th> Thursday </th>
<th> Friday </th>
<th> Saturday</th>
</tr>
<tr>
<td> 8:30 - 10:00 </td>
<td> Math</td>
<td>Math </td>
<td>Math</td>
<td> Math </td>
<td> Math</td>
<td> Math</td>
</tr>
</table>
如何仅使用JavaScript创建这样的表?
答案 0 :(得分:0)
<div id="output"></div>
<script>
var days = {
"Monday": ["8:30 - 10:00", "Math", "Math", "Math", "Math", "Math", "Math"],
"Tuesday": [],
"Wednesday": [],
"Thursday ": [],
"Friday": [],
"Saturday": []
}
var html;
html = "<table><tr>"
for (var day in days) {
html += "<th>" + day + "</th>";
}
html += "</tr>"
for (var day in days) {
if (days[day].length != 0) {
html += "<tr>"
for (var i = 0; i < days[day].length; i++) {
html += "<td>" + days[day][i] + "</td>"
}
}
html += "</tr>";
}
html += "</table>";
document.getElementById("output").innerHTML = html;
</script>