八、事件绑定函数

八、事件绑定函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
function bindEvent(dom, type, fn){
// 使用能力检测,检测浏览器支持哪种能力
// 能力检测就是利用:当一个元素读取一个属性的时候,如果能读取到,那么就输出,如果属性不存在就会输出undefined,而不是报错的这个特点
if (dom.addEventListener) {
// 存在addEventListener,说明是高级浏览器
dom.addEventListener(type, fn, false);
} else if (dom.attachEvent){
// 说明是IE中的高版本
dom.attachEvent("on" + type, fn);
} else {
// 说明是IE中的低版本或一些不知名的浏览器
dom["on" + type] = fn;
}
}

// 调用
bindEvent(box, "click", function(){
console.log("事件执行了");
});

UMn9vq.png

点击查看

本文标题:八、事件绑定函数

文章作者:Mango

发布时间:2020年07月13日 - 22:23:37

最后更新:2020年07月14日 - 13:27:10

原始链接:https://mango185.github.io/post/3c88a03c.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------