IE 8 想用ES5 ES6的一些JS方法 需要做一些兼容
1. 兼容array.forEach
if (!Array.prototype.forEach && typeof Array.prototype.forEach !=="function") {
Array.prototype.forEach=function(callback, context) {
if (Object.prototype.toString.call(this)==="[object Array]") {
var i,
len;
for (i=0, len=this.length;
i < len;
i++) {
if (typeof callback==="function" && Object.prototype.hasOwnProperty.call(this, i)) {
if (callback.call(context, this[i], i, this)===false) {
break; // or return;
}
}
}
}
}
}
2. 兼容node.getElementsByClassName
function getElementByClassName(node, className) {
if (node.getElementsByClassName) {
return node.getElementsByClassName(className);
} else {
var resultArr = [];
var eles = document.getElementsByTagName("*");
for (var iEle = 0; iEle < eles.length; iEle++) {
var clsName = eles[iEle].getAttribute("class");
if (clsName == className) {
resultArr.push(eles[iEle]);
}
}
return resultArr;
}
}
网友评论