[英]How to dynamically insert div in a table cell?
我正在嘗試將具有動態ID的div添加到表單元格中。
我現在是這樣的:
var ringNr = 1;
function tableCreate(){
alert('tableCreate');
var newDiv = document.createElement('div');
newDiv.id = "ring0" + ringNr;
newDiv.style.height = "40px";
newDiv.innerHTML = "Hello";
$("#shoppingCart tbody").append(
"<tr>"+
"<td>"+this.appendChild(newDiv)+"</td>"+ //This line creates the error
//More cells to add
"</tr>");
ringNr++;
};
this.appendChild(newDiv)引發以下錯誤: Uncaught TypeError: Object [object global] has no method 'appendChild'
如何將這個div添加到表單元中而不會出現錯誤?
看這段代碼:
"<tr>"+
"<td>"+this.appendChild(newDiv)+"</td>"+ //This line creates the error
//More cells to add
"</tr>");
您正在構建一個字符串。 如果要追加,則需要創建一個元素。
var tr = $("<tr/>");
var td = $("<td/>");
td.append(newDiv);
tr.append(td);
$("#shoppingCart tbody").append(tr);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.