[英]Deleting all the clone node of a table row in javascript
function newRow(t) {
var parent = t.parentNode.parentNode.parentNode;
var row = t.parentNode.parentNode.cloneNode(true);
row.firstChild.nextSibling.firstChild.setAttribute('value', 'sumit');
parent.appendChild(row);
}
function removeRow(t) {
var y = t.parentNode.parentNode;
y.parentNode.removeChild(y);
}
the above code is working fine but i want to delete all the clones at once which are created by above code on a onchange event of a select box 上面的代码工作正常,但我想一次删除所有由上面的代码在选择框的onchange事件上创建的克隆
Just add a class name to the cloned elements which would allow you to search for them and delete them later: 只需将一个类名添加到克隆的元素中,这将允许您搜索它们并在以后将其删除:
var row = t.parentNode.parentNode.cloneNode(true);
row.className += ' clonedrow';
...
// Remove all the cloned rows
var clonedRows = document.querySelectorAll('.clonedrow');
for (var i = 0; i < clonedRows.length; i++) {
clonedRows[i].parentNode.removeChild(clonedRows[i]);
}
Once the cloned rows have been added to the DOM, they look (to JavaScript) just like the original rows. 将克隆的行添加到DOM后,它们(对于JavaScript)看起来就像原始行一样。
Since you're just appending cloned rows to the table body, what I would do is as follows: 由于您只是将克隆的行添加到表主体,因此我将执行以下操作:
.childNodes.length
to get the number of rows in your table and store it globally in, say, lastpos
. .childNodes.length
获取表中的行数,并将其全局存储在例如lastpos
。 .childNodes[lastpos]
and remove nodes until the table has lastpos
rows again. .childNodes[lastpos]
并删除节点,直到表再次具有lastpos
行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.