前些天做兼容的时候,一些低版本的浏览器NodeList不支持forEach函数。
- 解决方案
为不支持forEach函数的浏览器手动添加forEach函数
/**
* 为nodelist添加forEach函数
*/
function addForEachToNodeList () {
if (window.NodeList && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this)
}
}
}
}
原文章链接 https://developer.mozilla.org/en-US/docs/Web/API/NodeList/forEach