Dcumentオブジェクトの属性と方法の互換性がない問題
3634 ワード
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)));
};
};
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;
}
}
function myAddEvent(obj, ev, fn) {
if (obj.attachEvent) {
obj.attachEvent("on" + ev, fn);
}
else {
obj.addEventListener(ev, fn, false);
}
}
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;
}
function stopPropagation(){
e = window.event || e;
if (document.all) { // ie
e.cancelBubble = true;
} else {
e.stopPropagation();
}
}
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;
}