【发布时间】:2023-03-04 00:27:02
【问题描述】:
考虑这段代码:
xr = document.querySelectorAll('.material-tooltip'); // NodeList
console.log(xr.length); // 50
for (ya of xr)
ya.parentNode.removeChild(ya);
zu = document.querySelectorAll('.material-tooltip');
console.log(zu.length); // 0
这按预期工作,它删除了所有找到的元素。现在考虑这个 代码:
xr = document.getElementsByClassName('material-tooltip'); // HTMLCollection
console.log(xr.length); // 50
for (ya of xr)
ya.parentNode.removeChild(ya);
zu = document.getElementsByClassName('material-tooltip');
console.log(zu.length); // 25
它只删除了找到的元素的一半。这是什么原因造成的?
【问题讨论】:
标签: javascript removechild getelementsbyclassname nodelist htmlcollection