我来自python背景,所以如果这听起来很愚蠢,那么请允许我批评。但是我有三个元素,我已经将它们存储在单独的变量中,然后从这些变量中创建一个数组。如何将dom元素传递给javascript函数?
var item = document.getElementById("s-tools");
var item2 = document.getElementById("s-tools2");
var item3 = document.getElementById("s-tools3");
var arr = [item, item2, item3]
现在我想在我for循环中迭代这个dom对象的数组,并从这些项中移除子元素。
for (var item in arr) {
while (item.hasChildNodes()) {
item.removeChild(item.lastChild);
}
}
则在项目抛出下面的错误和值为0
Uncaught TypeError: item.hasChildNodes is not a function
请分享html代码。同样使用'for .. in'对于迭代数组是个不错的主意 https://stackoverflow.com/questions/500504/why-is-using-for-in- with-array-iteration-a-bad-idea – brk
[Uncaught TypeError:Can not call method'hasChildNodes'of undefined]可能的重复(https://stackoverflow.com/questions/14681051/uncaught-typeerror-cannot-call-method -haschildnodes-of-undefined) – tousif