forEach循环应该在IE11和diplay中工作。
对象不支持属性或方法“forEach”。
它应该可以工作,因为它是一个ECMAScript-5函数和IE11支持它。
但是,这里的代码不起作用:
var alltable = document.querySelectorAll('*[id^="table_"]'); //Select all elements with the id starting by "table_"
alltable.forEach(function(element) {
// Do some code
});知道为什么吗?
发布于 2019-04-18 08:12:16
好吧我自己
forEach()实际上正在开发IE11,只是在如何称呼它时要小心。
querySelectorAll()是一个返回NodeList的方法。在Internet上,foreach() 只对数组对象工作。(它与NodeList和ES6,(不受IE11支持)一起工作。
要解决这个问题,一些人会建议使用一个多边形填充,它可以工作得很好,但是您也可以简单地使用slice.call()方法将您的slice.call()转换成一个数组:(在此解释)
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
});或者:
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
});总之:一定要将它用于数组对象,而不是NodeList.
希望能帮上忙。
https://stackoverflow.com/questions/55741747
复制相似问题