js常见兼容

滚动条:

1
document.documentElement.scrollTop||document.body.scrollTop

获取样式兼容

1
2
3
4
5
function getStyle(dom, styleName){
return dom.currentStyle?
dom.currentStyle[styleName]:
getComputedStyle(dom)[styleName];
}

网页可视区域兼容

1
2
window.innerHeight || document.documentElement.clientHeight
window.innerWidth || document.documentElement.clientWidth

事件对象兼容

1
2
evt = evt || window.event;
event.target.className

阻止事件冒泡兼容

1
2
3
event.stopPropagation?
event.stopPropagation():
event.cancelBubble=true;

阻止默认行为兼容

1
2
3
evt.preventDefault?
evt.preventDefault():
evt.returnValue=false;

事件监听兼容

1
2
3
document.all?
dom.attachEvent(“onclick”, fn):
dom.addEventListener(“click”, fn);

事件委托目标对象兼容

1
2
var t = event.target || event.srcElement;
if(t.nodeName.toLowerCase() == “li”) {}

ajax请求的兼容

1
2
3
4
5
var http;
window.XMLHttpRequest?
http = new XMLHttpRequest():
http = new AtiveXObject('Microsoft.XMLHTTP');
`