3 回答
TA贡献1851条经验 获得超3个赞
我想这可以解决问题:
const spanc = document.querySelectorAll('.div1 .spanc'); spanc.forEach(node => node.remove());
TA贡献1807条经验 获得超9个赞
因此,问题的一部分是您尝试删除的跨度具有不同的父元素;有时它是一个跨度,否则它是一个div。
const itemsToRemove = document.getElementsByClassName('spanc');
const parents = document.getElementsByClassName('spanp');
[...parents].forEach(span => {
const itemsToRemove = span.getElementsByClassName('spanc');
[...itemsToRemove].forEach(item => span.removeChild(item));
});
const outerParent = document.getElementsByClassName('div1')[0];
const upperItemsToRemove = outerParent.getElementsByClassName('spanc');
[...upperItemsToRemove].forEach(item => outerParent.removeChild(item));
<div class="div1">
<span class="spanp">span1<span class="spanc">spanchild1</span></span>
<span class="spanc">spanc</span>
<span class="spanp">span2<span class="spanc">spanchild1</span></span>
<span class="spanc">spanc</span>
<span class="spanp">span2<span class="spanc">spanchild1</span></span>
<span class="spanc">spanc</span>
</div>
TA贡献1895条经验 获得超7个赞
我通过使用 while 循环而不是 forloop 得到了答案
while(list.length>0){
let i=0;
list[i].parentNode.removeChild(list[i]);
}
- 3 回答
- 0 关注
- 110 浏览
添加回答
举报