2017-01-01 47 views
1

我目前正在開發一個遊戲大廳,顯示當前連接到遊戲的玩家列表。爲了向表中添加新玩家,我正在使用JavaScript insertRowinsertCell API。我寫了下面的功能添加到表:動態添加具有某個類名稱的表格單元

function tabappend(playernam, sockid, ready) { 
    // playerlist is the ID of the table 
    var tab = document.getElementById('playerlist'); 
    var row = tab.insertRow(-1); // append to table 
    var cell0 = row.insertCell(0); // our three columns 
    var cell1 = row.insertCell(1); 
    var cell2 = row.insertCell(2); 

    // populate the newly created row 
    cell0.innerHTML = playernam; 
    cell1.innerHTML = sockid; 

    // if the player is ready to play, add a checkmark and make 
    // the class name of the cell "ready". Otherwise, the cell 
    // should be empty 
    if (ready == true) { 
     cell2.innerHTML = "✓"; 
     cell2.class = "ready"; 
    } 
} 

三個參數表中的每一行的三個單元:球員的名字,插座ID,以及玩家是否已經準備好打或不。

但是,如果玩家已準備好玩,那麼我希望該行中的第三個單元的類名稱爲ready。我怎麼做到這一點?上面的代碼顯示了我的嘗試,但沒有奏效。

+0

@epascarello,我認爲這是一個更好的:http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript問題不是關於追加,而是關於設置 – Dekel

回答

2

className你正在尋找

cell2.className = "ready"; 
相關問題