今天用了js的forEach函数,发现IE9一下的浏览器都不支持forEach函数,会报 对象不支持“forEach”属性或方法。
上网查了下,可以加上下面一段代码来支持这个函数:
if (!Array.prototype.forEach)
{
Array.prototype.forEach = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
fun.call(thisp, this[i], i, this);
}
};
}
本文介绍了在Internet Explorer 9及以下版本中使用Array.prototype.forEach方法遇到的问题,并提供了一个简单的polyfill来解决此问题,使得开发者可以在不支持该方法的老版本浏览器中正常使用此功能。
675

被折叠的 条评论
为什么被折叠?



