当前位置:网站首页>JS手写兼容性事件绑定
JS手写兼容性事件绑定
2022-04-23 05:57:00 【zjLOVEcyj】
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
}
}
}
版权声明
本文为[zjLOVEcyj]所创,转载请带上原文链接,感谢
https://blog.csdn.net/cyj5201314/article/details/124016637
边栏推荐
猜你喜欢
随机推荐
数据可视化进一步学习
Your brain expands and shrinks over time — these charts show how
百度地图基础案例
C language structure specifying initialization
Informatics one book pass - small ball
Database programming of node
js中的作用域与作用域链
Set与Map
WebSocket(基础)
若依框架从零开始
出入库与库存系统的模型问题
【ORACLE与mysql的区别】
.Net Core 下使用 Quartz —— 【2】作业和触发器之初步了解作业
常用网站汇总
ASP.NET CORE3.1 Identity注册用户后登录失败的解决方案
Header内置对象
Arm common assembly instructions
el-table添加序号
说说ts的心里话
Redux概述