1 回答
TA贡献2041条经验 获得超4个赞
每次分配给时都会覆盖该元素innerHTML,而不是附加到它。您不能像使用 DOM 方法那样逐步构建 HTML,因为每次分配给innerHTML它时都会将其解析为完整的 HTML 片段。
innerHTML您应该改为附加到一个字符串,并在末尾分配给。
function print() {
// console.log("?")
var time = Number(localStorage.getItem("time"))
// console.log(typeof time)
var nClass = Number(localStorage.getItem("nClass"))
var duration = localStorage.getItem("duration")
var subjectsRaw = localStorage.getItem("arr")
var subjects = JSON.parse(subjectsRaw)
var html = "<table border = 1>"
for (var i = 0; i < 5; i++) {
console.log('test')
html += "<tr>"
for (let j = 0; j < nClass; j++) {
console.log('test2')
html += '<td>' + subjects[Math.floor(Math.random() * subjects.length + 0)] + '</td>'
}
html += "</tr>"
}
html += "</table>"
document.getElementById("tt").innerHTML = html;
}
添加回答
举报