虽说jQuery等各种框架对js的兼容做了处理,但在学习js过程中还是有必要对原生js一些兼容写法做些了解,下面是个人总结的一些原生js的一些兼容写法,希望对大家的js学习起到帮助!
1.获取默认参数
e || window.event
arguments[0]
2.阻止默认事件
e.preventDefault() || e.returnValue = false
return false
3.判断按键
e.keyCode || e.which
4.阻止冒泡
event.stopPropagation() || event.cancleBubble = true
5.获取节点
nextSibling || nextEventSibling
6.滚动条的偏移量
document.documentElement.scrollTop || document.body.scrollTop
7.获取样式
dom.currentStyle["width"] || getComputedStyle(dom)["width"]
8.事件监听
function addEventListener(obj(监听对象),type(监听事件类型),fn(执行函数),capture(false冒泡默认)){
if(obj.addEventListener){
obj.addEventListener(type,fn,capture)
}else{
obj.attachEvent("on"+type,fn)
}
}
《完》