1.滚动条到顶端的距离
var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
2.滚动条到左端的距离
var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
3.通过class属性获取页面元素对象的兼容
function byClassName(obj,className){
if(obj.getElementsByClassName){
return obj.getElementsByClassName(className);
}else{
var arr = [];
var eles = obj.getElementsByTagName(‘*‘);
for(var i = 0; i < eles.length; i++){
if(eles[i].className === className){
arr.push(eles[i]);
}
}
return arr;
}
}
4.获取非行内样式的兼容
function getStyle(obj,attr){
return obj.currentStyle ? obj.currentStyle[attr] : window.getComputedStyle(obj.1)[attr];
}
5.获取事件对象的兼容
var e = event || window.event;
6.获取鼠标编码值的兼容
function getButton(event){
var e = event || window.event;
if(event){
return e.button;
}else if(window.event){
switch(e.Button){
case 1 : return 0;
case 4 : return 1;
case 2 : return 2;
}
}
}
七,获取键盘编码值的兼容
var code = event.keyCode || event.charCode || event.which;
八.阻止时间冒泡的兼容
event.stopPropagation ? event.stopPropagation() : event.cancelBubble = true;
九,阻止超链接的默认行为
event.preventDefault ? event.preventDefault() : event.returnValue = false;
十,添加时间监听器
function addEventListener(obj,event,fn,boo){
if(obj.addEventListener){
obj.addEventListener(event,fn,boo);
}else if(obj.attachEvent){
obj.attachEvent(‘on‘ + event,fn);
}
}
十一,移除时间监听器
function removeEventListener(obj,event,fn,boo){
if(obj.removeEventListener){
obj.removeEventListener(event,fn,boo);
}else if(obj.detachEvent){
obj.detachEvent(‘on‘ + event,fn)
}
}
十二,获取事件源的兼容
var target = e.target || e.srcElement;
原文地址:https://www.cnblogs.com/arguments/p/10319841.html