javascriptオブジェクトを含む表に新しい行を挿入しようとしています。JavaScriptで表に行を挿入する
HTMLは次のようになります。
var table = document.getElementById("userTable");
var tr = document.createElement("tr");
var td = document.createElement("td");
var fn = document.createTextNode(user.firstname);
var ln = document.createTextNode(user.lastname);
var score = document.createTextNode(user.score);
td.appendChild(fn);
td.appendChild(ln);
td.appendChild(score);
tr.appendChild(td);
table.appendChild(tr);
<table id="userTable">
<thead>
<th>First Name</th>
<th>Last Name</th>
<th>Score</th>
</thead>
<tbody>
</tbody>
</table>
私がいる問題は見出しに別々のTDの対応とは対照的に、現在、それは、1つのTDにすべてのデータを追加することです。どのようにデータを別々のセルに追加できますか?テーブルを作成するために
insertRow()
とinsertCell()
:
'document.createElement(" td ")'のように複数のtd要素を作成する必要があります。 – xander
ユーザーには何が含まれていますか? – moon
あなたは単なるtd要素を作成しました。そしてテーブルに追加する3つの変数があります。変数名td1、td2、td3で3つのtdを作成し、それらをテーブルに追加する必要があります。 –