ie9以下的浏览器兼容性问题

.bind不兼容的问题
Function.prototype.bind = function () {
var fn = this, args = Array.prototype.slice.call(arguments), object = args.shift();
return function () {
return fn.apply(object,
args.concat(Array.prototype.slice.call(arguments)));
};
};


.getElementsByClassName 不兼容的问题

var getElementsByClassName = function (searchClass, node, tag) {
var result = [];
if (document.getElementsByClassName) {
var nodes = (node || document).getElementsByClassName(searchClass);
for (var i = 0; node = nodes[i++];) {
if (tag !== "*" && node.tagName === tag.toUpperCase()) {
result.push(node)
}
}
return result
} else {
node = node || document;
tag = tag || "*";
var classes = searchClass.split(" "),
elements = (tag === "*" && node.all) ? node.all : node.getElementsByTagName(tag),
patterns = [],
current,
match;
var i = classes.length;
while (--i >= 0) {
patterns.push(new RegExp("(^|\\s)" + classes[i] + "(\\s|$)"));
}
var j = elements.length;
while (--j >= 0) {
current = elements[j];
match = false;
for (var k = 0, kl = patterns.length; k < kl; k++) {
match = patterns[k].test(current.className);
if (!match) break;
}
if (match) result.push(current);
}
return result;
}
}

addEventListener不兼容的问题

function myAddEvent(obj, ev, fn) {
if (obj.attachEvent) {
obj.attachEvent("on" + ev, fn);
}
else {
obj.addEventListener(ev, fn, false);
}
}

.map不兼容的问题
Array.prototype.myMap = function (fn, context) {
context = context || window;
var ary = [];
if (Array.prototype.map) {
ary = this.map(fn, context);
} else {
for (var i = 0; i < this.length; i++) {
ary[i] = fn.apply(context, [this[i], i, this]);
}
}
return ary;
}

e.stopPropagation的兼容性问题

function stopPropagation(){
e = window.event || e;
if (document.all) { //只有ie识别
e.cancelBubble = true;
} else {
e.stopPropagation();
}
}

e.currentTarget的兼容性问题修复如下:
var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判断是否IE浏览器
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if (fIEVersion == 7 || fIEVersion == 8) {
e=e.srcElement.parentElement;
}else{
e=e.currentTarget;
}
}else{
e=e.currentTarget;
}
posted @ 2017-02-27 15:44  忆如梨花  阅读(1233)  评论(0编辑  收藏  举报