简体   繁体   English

为什么Chrome抛出'getAttribute'不是for循环内的函数错误?

[英]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'));
}

Array.from--Mozilla Array.from--Mozilla

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 ): 工作片段(注意,在控制台中,我们可以有两个属性lengthitem 。对于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.

相关问题 为什么Chrome接受Firefox,Firefox却为文字{{a“:1}抛出错误? - Why does Firefox throw an error for the literal {“a”: 1} while Chrome accepts it? 为什么三元运算符中的逗号会在JavaScript中引发语法错误? - Why does this comma inside a ternary operator throw a syntax error in JavaScript? 为什么调用去抖动的函数会引发“不是函数”错误? - Why does calling a debounced function throw “is not a function” error? 带有等待功能循环的抛出错误 - Throw Error with loop of await function Javascript —为什么getAttribute()函数不适用于某些属性? - Javascript — Why does the getAttribute() function not work on certain attributes? 为什么 function 内的 while 循环不起作用? - Why function inside while loop does not work? 为什么这个函数在使用includes方法的时候会报错? - Why does this function throw an error when using the includes method? 为什么在eval()中的typeof会在我的函数中抛出错误? - Why does typeof within eval() throw an error in my function? 为什么在reduce 函数中返回局部变量的长度会引发错误? - Why does returning the length of a local variable in a reduce function throw an error? 当 function 参数类型不匹配时,为什么 TypeScript 不抛出错误? - Why does TypeScript not throw an error when the function argument type is mismatched?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM