2017-01-01 99 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"; 
相关问题