2017-01-01 10 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"; 
    } 
} 

3つの引数がテーブルの各行の3個の細胞である:選手名、ソケットID、およびプレーヤーがプレーする準備ができているかどうか。

しかし、プレイヤーがプレイする準備ができている場合、その行の3番目のセルのクラス名はreadyになります。私はこれをどのように実現するのでしょうか?上記のコードは私の試みを示していますが、うまくいきませんでした。

+0

@epascarelloを探している、私はこの1つが優れていると思う:http://stackoverflow.com/questions/195951/change-an-elements-class-with-javascript質問は追加に関する問題ではなく、セットに関する – Dekel

答えて

2

そのclassNameあなたは

cell2.className = "ready"; 
関連する問題