这是初学者的问题...
我想写一些代码来初始化我网页中的一堆座位图片,
为它们创建并设置属性, 并让他们每 9 个座位(4 排,每排有 9 个座位)换行,这是代码
function initSeats() {
var seatsDiv = document.getElementById("seats");
//Initialize the appearence of all seats
for (var i = 0; i < seats.length; i++) {
for (var j = 0; j < seats[i].length; j++) {
var currentSeatIndex = i * seats[i].length + j;
if (seats[i][j]) {
//if current seat is available(true), create a new IMG element and set some attributes;
} else {
//if current seat is unavailable(true), create a new IMG element and set some attributes;
}
}
seatsDiv.appendChild("<br>");//here is the problem
}
}
我想做的是当其中一个外层循环结束时,在末尾添加一个
,
但后来我在 Chrome 中收到一个“NotFoundError”,看起来节点 seatsDiv 不存在
所以毕竟只有一排座位初始化成功。
appendChild 是否应该在该位置附加任何内容?还是应该使用其他方法?
最佳答案
appendChild
需要一个 HTMLElement 而不是一个字符串,尝试切换到:
seatsDiv.appendChild(document.createElement("br"));
关于javascript - 当我在真正存在的引用中使用 appendChild ("<br>"时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19089171/