IE8 数组的indexOf方法兼容
今天测试完IE的布局,开始点击查看逻辑有没有bug,发现提示我没有indexof的方法;
查了百度才知道,IE8下,数组是不支持indexOf的方法的:
可以对Array添加indexOf方法,方便调用,如下:
if (!Array.prototype.indexOf){
Array.prototype.indexOf = function(elt /*, from*/){
var len = this.length >>> 0;
var from = Number(arguments[1]) || 0;
from = (from < 0)
? Math.ceil(from)
: Math.floor(from);
if (from < 0)
from += len;
for (; from < len; from++)
{
if (from in this &&
this[from] === elt)
return from;
}
return -1;
};
}