常见的一些JS兼容问题

事件监听的兼容写法:
if(box.addEventListener){ //高版本浏览器
        box.addEventListener("click",function()
        },false)
    }else{//ie
        box.attachEvent("onclick",function(){
        })
    }
获取事件源的:
 var target = e.target || e.srcElement
计算内部偏移量的:
var disx = e.offsetX  ||  e.layerX
var disy = e.offsetY  ||  e.layerY
获取页面滚走的距离:
window.onscroll = function(){
    document.body.scrollTop || document.documentElement.scrollTop
}
获取时间对象
e = e || window.event
键盘事件
var x = event.which || event.keyCode;
阻止事件冒泡兼容:
e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
非行内样式
兼容if(window.getComputedStyle){ //高版本浏览器window上有getComputedStyle
        window.getComputedStyle(box,null).width
    }else{
        box.currentStyle.width;
    }
阻止浏览器默认行为
e.preventDefault ? e.preventDefault() : e.returnValue=false;
原文地址:https://www.cnblogs.com/qhya/p/10495713.html