使用Javascript在TR元素中创建TD元素

使用Javascript在TR元素中创建TD元素,javascript,Javascript,每次运行此脚本时: document.getElementById("billTable").appendChild(nextRow) document.getElementById("billTable").appendChild(LISTname); document.getElementById("billTable").appendChild(LISTprice); document.getElementById("billTable").appendChild(LISTremove);

每次运行此脚本时:

document.getElementById("billTable").appendChild(nextRow)
document.getElementById("billTable").appendChild(LISTname);
document.getElementById("billTable").appendChild(LISTprice);
document.getElementById("billTable").appendChild(LISTremove);
它创造了:

<tr></tr><td>L - Meat Lovers Pizza</td><td>$6.70</td><td>X</td>

我正在尝试使TD标签与TR一起使用。

使用insertCell

var table = document.getElementById("myTable");
var row = table.insertRow(0);

var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);

cell1.innerHTML = "List Name";
cell2.innerHTML = "List Price";
nextRow似乎包含tr元素,因此您应该附加到该元素,而不是表:

nextRow.appendChild(LISTname);
nextRow.appendChild(LISTprice);
nextRow.appendChild(LISTremove);
document.getElementById("billTable").appendChild(nextRow);
我假设billTable是,如果是这样,那么您针对的是错误的elkement,您必须针对您需要将td元素附加到tr而不是表中的元素