class bindEvent {
constructor (element) {
this.element = element
}
// 绑定
addEventListener (type, handler) {
if (this.element.addEventListener) {
this.element.addEventListener(type, handler, false)
} else if (this.element.attachEvent) {
this.element.attachEvent('on'+type, handler, false)
} else {
this.element['on'+type] = handler
}
}
// 解绑
removeEventListener (type, handler) {
if (this.element.removeEventListener) {
this.element.removeEventListener(type, handler, false)
} else if (this.element.detachEvent) {
this.element.detachEvent('on'+type, handler, false)
} else {
this.element['on'+type] = null
}
}
// 阻断
static stopPropogation (e) {
if (e.stopPropogation) {
e.stopPropogation
} else {
e.cancelBubble = true
}
}
// 默认拦截
static preventDefault (e) {
if (e.preventDefault) {
e.preventDefault
} else {
e.returnValue = false
}
}
}
JS手写兼容性事件绑定
最新推荐文章于 2023-10-31 11:36:19 发布