js常见兼容

滚动条的兼容写法(谷歌chrome)
document.documentElement.scrollTop || document.body.scrollTop
 
阻止浏览器默认行为的兼容写法
 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
 
获取非行内样式的兼容写法(不是浏览器兼容写法)(IE)
getComputedStyle ? window.getComputedStyle(obj)[attr] : obj.currentStyle[attr];
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");
}

猜你喜欢

转载自www.cnblogs.com/tis100204/p/10297517.html