在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);
}
上面的代码工作正常,但我想立即删除上面代码在选择框的 onchange 事件上创建的所有克隆
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需向克隆元素添加一个类名,这样您就可以搜索它们并稍后删除它们:
Just add a class name to the cloned elements which would allow you to search for them and delete them later:
将克隆行添加到 DOM 后,它们(对于 JavaScript)看起来就像原始行一样。
由于您只是将克隆的行附加到表主体,所以我要做的如下:
.childNodes.length
获取行数并将其全局存储在lastpos
中。.childNodes[lastpos]
开始并删除节点,直到表再次出现lastpos
行。Once the cloned rows have been added to the DOM, they look (to JavaScript) just like the original rows.
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[lastpos]
and remove nodes until the table haslastpos
rows again.