2017-02-16 87 views
-3

我每次运行此脚本:创建一个TR元素中的TD元素使用Javascript

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。

+0

我想'billTable'是'

',如果是这样,你的目标是错误的设置,你必须锁定'' – zer00ne

+0

你需要将'td'元素附加到'tr'而不是'table' – jmoerdyk

回答

2

看来,nextRow包含tr元素,所以你应该追加到的是,不表:

nextRow.appendChild(LISTname); 
nextRow.appendChild(LISTprice); 
nextRow.appendChild(LISTremove); 
document.getElementById("billTable").appendChild(nextRow); 
0

使用的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";