47 lines
1.0 KiB
Markdown
47 lines
1.0 KiB
Markdown
# 20190925
|
||
|
||

|
||
|
||
* 给任意元素添加监听器封装函数
|
||
```js
|
||
function addEventListener(ele,type,fn) {
|
||
if(ele.addEventListener) {
|
||
ele.addEventListener(type,fn,false)
|
||
} else if (ele.attachEvent) {
|
||
ele.attachEvent('on' + type, fn)
|
||
} else {
|
||
ele['on' + type] = fn
|
||
}
|
||
}
|
||
|
||
```
|
||
* 给任意事件解绑监听器封装函数
|
||
|
||
```js
|
||
//如需解绑监听器,则添加事件必须为命名函数
|
||
function removeEventListener(ele, type, fn) {
|
||
if(ele.removeEventListener) {
|
||
ele.removeEVentListener(type, fn, false)
|
||
} else if (ele.detachEvent) {
|
||
ele.detachEvent('on' + type, fn)
|
||
} else {
|
||
ele['on' + type] = fn
|
||
}
|
||
}
|
||
|
||
```
|
||
|
||
* 阻止事件冒泡
|
||
|
||
```js
|
||
//e | window.event :事件参数对象,前者是w3c,后者IE
|
||
|
||
//google,ie8 | firefox不支持
|
||
//火狐不支持 window.event
|
||
window.event.cancelBubble = true;
|
||
e.cancelBubble = true;
|
||
|
||
//w3c firefox,google | ie8不支持
|
||
e.stopPropagation();
|
||
|
||
``` |