兼容写法

 滚动条的兼容写法(谷歌chrome)
获取滚动条上距离和左距离
var scrolltop =document.documentElement.scrollTop || document.body.scrollTop
var scrollleft =document.documentElement.scrollLeft || document.body.scrollLeft
                            兼容非谷歌                                                             兼容谷歌
 
阻止浏览器默认行为的兼容写法
 e.preventDefault?e.preventDefault():e.returnValue=false;  或者   return false;
 
获取事件源的兼容写法
 target = e.target || e.srcElement;  
 
键盘事件的兼容写法
e.keyCode || e.which
 
阻止事件冒泡的兼容写法
e.stopPropagation?e.stopPropagation():e.cancelBubble = true
 
事件对象的兼容写法
var evt = e || event  或var evt = e || window.event
 
获取非行内样式的兼容写法(不是浏览器兼容写法)(IE)
getComputedStyle ? window.getComputedStyle(obj)[attr] : obj.currentStyle[attr];//IE
if( window.getComputedStyle ){
   alert( window.getComputedStyle( divs[0] )["backgroundColor"] );
}else{
   alert( divs[0].currentStyle["height"] );
}
 
创建ajax对象的兼容
window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");
if( window.XMLHttpRequest ){
    var ajax = new XMLHttpRequest();
}else{
    var ajax = new ActiveXObject("Microsoft.XMLHTTP");
}

IE判定

IE6
if(!-[1,]) { //IE6
     
} else{ //非IE6
 
}
 
IE6/7/8
if(window.VBArray){  //IE678
 
} else {
 
原文地址:https://www.cnblogs.com/mortalway/p/12023716.html