admin管理员组文章数量:1332388
forEach loops are supposed to be working in IE11 and diplay
Object doesn't support property or method 'forEach'.
It should be working since it's an ECMAScript-5 function and IE11 supports it.
However, my code here is not working:
var alltable = document.querySelectorAll('*[id^="table_"]'); //Select all elements with the id starting by "table_"
alltable.forEach(function(element) {
// Do some code
});
Any idea why ?
forEach loops are supposed to be working in IE11 and diplay
Object doesn't support property or method 'forEach'.
It should be working since it's an ECMAScript-5 function and IE11 supports it.
However, my code here is not working:
var alltable = document.querySelectorAll('*[id^="table_"]'); //Select all elements with the id starting by "table_"
alltable.forEach(function(element) {
// Do some code
});
Any idea why ?
Share Improve this question asked Apr 18, 2019 at 8:12 GawetGawet 3154 silver badges15 bronze badges 3- the title is a bit misleading ... – Nina Scholz Commented Apr 18, 2019 at 8:17
- 1 Possible duplicate of Why is forEach method breaking in IE? – delinear Commented Apr 18, 2019 at 8:19
- 1 Well, how would you call it ? That was my problem and I just wanted to help other who encounter the same... – Gawet Commented Apr 18, 2019 at 8:19
1 Answer
Reset to default 7Well myself,
forEach() is actually working on IE11, just be careful on how you call it.
querySelectorAll() is a method which return a NodeList. And on Internet Explorer, foreach() only works on Array objects. (It works with NodeList with ES6, not supported by IE11).
To fix this, some would advice a polyfill, which could work great, but you can also simply convert your NodeList into an array with the slice.call() method: (Explained here)
var alltable = document.querySelectorAll('*[id^="table_"]'); //Select all elements with the id starting by "table_"
var alltableArray= Array.prototype.slice.call(alltable);
alltableArray.forEach(function(element) {
// Do some code
});
Or:
var alltable = Array.prototype.slice.call(document.querySelectorAll('*[id^="table_"]')); //Select all elements with the id starting by "table_"
alltable.forEach(function(element) {
// Do some code
});
To sum up: Be sure you're using it on an Array object and not a NodeList.
Hope that can help someone.
本文标签: internet explorerJavascriptforEach() loops not working on IE11Stack Overflow
版权声明:本文标题:internet explorer - Javascript - forEach() loops not working on IE11 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742289793a2447581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论