您的位置:首页 > 其它

360极速浏览器中的极速模式下,报错:forEach is not a function

2017-08-10 11:17 441 查看
var eSpread, eWrap;
eWrap = document.querySelectorAll('.list-group-item[ng-repeat]');
eWrap.forEach(function(item) {
eSpread = document.createElement('i');
eSpread.classList.add('cus-glyphicon', 'glyphicon-menu-down');
eSpread.addEventListener('click', function(event) {
event.preventDefault();
event.stopPropagation();
angular.element(item).toggleClass('spread');
}, true);
item.appendChild(eSpread);
});


eWrap = document.querySelectorAll(‘.list-group-item[ng-repeat]’);

在谷歌和360极速浏览器的极速模式下,都显示eWrap是个nodeList,但在360下报错。

查过了一些资料,显示可以这么写,就不报错了

[].forEach.call(eWrap, function(item) { }


原参考答案是这么解释的:

Node.children is dom collection, not an real array so it doesn't have array methods like forEach(need to fix the case also).

So one commonly used solution is to call the array methods with the context as the html collection


我理解的意思是:

node.children是一个dom的集合,不是一个真正的数组,所以不能用数组的方法,比如forEach。

一个普遍有用的解决方法就是在HTML的集合的上下文里先声明是一个数组。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: