2013-09-30 84 views
1

這是一個初學者的問題,使用的appendChild(「
」)的錯誤發生...當我真正有了參考

我想要寫一些代碼來初始化一堆席位畫面在我的網頁,

創建並設置屬性給他們, ,使他們行改變每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應該在該位置追加任何東西嗎?或者我應該使用其他方法?

+2

的appendChild應給予一個節點,而不是一個字符串。 – Virus721

+0

@ Virus721非常感謝,看起來像我沒有正確的想法 –

回答

8

appendChild期待一個HTMLElement而不是字符串,請嘗試切換到:

​​
+0

非常感謝!有用 –