本文最后更新于 2024-03-23T16:32:22+00:00
兼容点:谷歌浏览器、IE 浏览器
使用方式:
1 2 3 4 5 6 7 8 9 10 11
| eventModel.addEventListener(ele, 'click', fn, boolean)
eventModel.removeEventListener(ele, 'click', fn)
eventModel.stopPropagation()
eventModel. preventDefault()
|
代码实现:就是在里面写不同浏览器支持的 API 而已
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| const eventModel = { addEventListener(ele, eventName, fn, flag) { if(ele.addEventListener) { ele.addEventListener(eventName, fn, flag) } else if(ele.addtachEvent) { ele.addtachEvent(`on${eventName}`, fn) } else { ele[`on${eventName}`] = fn } },
removeEventListener(ele, eventName, fn) { if(ele.removeEventListener) { ele.removeEventListener(eventName, fn) } else if(ele.detachEvent) { ele.addtachEvent(`on${eventName}`, fn) } else { ele[`on${eventName}`] = null } },
stopPropagation(event) { if(event.stopPropagation) { event.stopProgamation() } else { ele.cancelBubble = true } }
preventDefault(event) { if(event.preventDefault) { event.preventDefault() } else { ele.returnValue = false } } }
|