首页 > Web开发 > 详细

常见的一些JS兼容问题

时间:2019-03-08 15:05:13      阅读:162      评论:0      收藏:0      [点我收藏+]
事件监听的兼容写法:
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;

常见的一些JS兼容问题

原文:https://www.cnblogs.com/qhya/p/10495713.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!