[英]Why does chrome throw 'getAttribute' is not a function error inside a for loop?
With the following markup: 使用以下标记:
<div id="parent">
<div id="one">one</div>
<div id="two">two</div>
<div id="three">three</div>
<div id="four">four</div>
<div id="five">five</div>
<div id="six">six</div>
</div>
<p id="output">
</p>
and the following javascript: 和以下javascript:
var output = document.getElementById('output');
var items = document.querySelectorAll('#parent > div');
output.innerHTML += '#' + items[2].getAttribute('id') + '<br /><br />';
for(index in items){
output.innerHTML += '#' + items[index].getAttribute('id') + ': ';
output.innerHTML += items[index].innerHTML + '<br />';
}
Chrome throws the following error in the console: Chrome在控制台中引发以下错误:
Uncaught TypeError: items[index].getAttribute is not a function
未捕获的TypeError:items [index] .getAttribute不是函数
I have checked this in IE 11 and there are no errors in the console. 我已经在IE 11中进行了检查,控制台中没有错误。 What's weird is that the actual result of the expression is correct.
奇怪的是表达式的实际结果是正确的。
Does anyone know why this is happening? 有人知道为什么会这样吗?
var output = document.getElementById('output'); var items = document.querySelectorAll('#parent > div'); output.innerHTML += '#' + items[2].getAttribute('id') + '<br /><br />'; for(index in items){ output.innerHTML += '#' + items[index].getAttribute('id') + ': '; output.innerHTML += items[index].innerHTML + '<br />'; }
#output{ color: blue; }
<div id="parent"> <div id="one">one</div> <div id="two">two</div> <div id="three">three</div> <div id="four">four</div> <div id="five">five</div> <div id="six">six</div> </div> <p id="output"> </p>
Use a for
loop instead. 请使用
for
循环。 for in
loops work better with key value pairs like object literals. for in
循环与键值对(例如对象文字)更好地协作。 Your array-like object does not have inherited enumerated properties. 类数组对象没有继承的枚举属性。
A for...in loop only iterates over enumerable properties.
for ... in循环仅迭代可枚举的属性。
from MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in 来自MDN https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/for...in
var output = document.getElementById('output'); var items = document.querySelectorAll('#parent > div'); output.innerHTML += '#' + items[2].getAttribute('id') + '<br /><br />'; for (let index = 0; index < items.length; index++) { output.innerHTML += '#' + items[index].getAttribute('id') + ': '; output.innerHTML += items[index].innerHTML + '<br />'; }
#output { color: blue; }
<div id="parent"> <div id="one">one</div> <div id="two">two</div> <div id="three">three</div> <div id="four">four</div> <div id="five">five</div> <div id="six">six</div> </div> <p id="output"> </p>
convert var items
to this: 将
var items
转换为此:
var items = Array.from(document.querySelectorAll('#parent > div'));
querySelectorAll
is not returning array
but NodeList
instead. querySelectorAll
不返回array
而是返回NodeList
。
and because you work with IE also (which do not support Array.from). 并且因为您还使用IE(不支持Array.from)。 It is better to check it first, something like this:
最好先进行检查,如下所示:
if (!Array.from) {
//IE and else which do not support it
var items = document.querySelectorAll('#parent > div');
} else {
var items = Array.from(document.querySelectorAll('#parent > div'));
}
To get id you need to do [DOMNode].id : 要获取ID,您需要执行[DOMNode] .id:
Working Snippet: 工作片段:
var output = document.getElementById('output'); var items = document.querySelectorAll('#parent > div'); output.innerHTML += '#' + items[2].id + '<br /><br />'; for (index in items) { output.innerHTML += '#' + items[index].id + ': '; output.innerHTML += items[index].innerHTML + '<br />'; }
<div id="parent"> <div id="one">one</div> <div id="two">two</div> <div id="three">three</div> <div id="four">four</div> <div id="five">five</div> <div id="six">six</div> </div> <p id="output"> </p>
Update: 更新:
The reason that there are #undefined: undefined
being displayed in above snippet is that 在上面的代码段中显示
#undefined: undefined
的原因是
querySelectorAll() method returns all elements in the document that matches a specified CSS selector(s), as a static NodeList object
querySelectorAll()方法将文档中与指定CSS选择器匹配的所有元素作为静态NodeList对象返回。
We need to use length
property of this object to properly loop through all the matched elements. 我们需要使用此对象的
length
属性来正确循环所有匹配的元素。
Working snippet (Notice in console we can we two properties length
, item
. By for in loop we traversed through these too but id and innerHTML are undefined ): 工作片段(注意,在控制台中,我们可以有两个属性
length
和item
。对于for循环,我们也遍历了这些属性,但是id和innerHTML尚未定义):
var output = document.getElementById('output'); var items = document.querySelectorAll('#parent > div'); output.innerHTML += '#' + items[2].id + '<br /><br />'; for (var index = 0; index < items.length; index++) { output.innerHTML += '#' + items[index].id + ': '; output.innerHTML += items[index].innerHTML + '<br />'; } console.log(items);
<div id="parent"> <div id="one">one</div> <div id="two">two</div> <div id="three">three</div> <div id="four">four</div> <div id="five">five</div> <div id="six">six</div> </div> <p id="output"> </p>
Do not use for...in
to iterate over arrays. 不要使用
for...in
遍历数组。
Use a regular for loop 使用常规的for循环
for(var i = 0 ; i < items.length ; i++){
output.innerHTML += '#' + items[i].getAttribute('id') + ': ';
output.innerHTML += items[i].innerHTML + '<br />';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.