简体   繁体   English

使用Javascript将元素附加到单元格

[英]Append element to cell with Javascript

I have a table with two columns and I like to append the same element to my second column. 我有一个包含两列的表,我想将相同的元素添加到第二列。 I tried it with the following code, but it always just adds it to the first row. 我用下面的代码尝试了它,但它始终只是将其添加到第一行。

for (var i = 0, row; row = table.rows[i]; i++) {
   var cell = row.cells[1];
   cell.appendChild(element1);
};

You need to clone the Node : 您需要克隆Node

cell.appendChild(element1.cloneNode(true))

true means deep copy here. true表示此处是深复制。 If you don't create a new Node, it just moves to the new position of the DOM tree. 如果您不创建新的节点,它只会移动到DOM树的新位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM