原生js 编写的事件监听

前端之家收集整理的这篇文章主要介绍了原生js 编写的事件监听前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<table class="text"><tr class="li1">
<td class="ln"><pre class="de1">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

代码!!!   ``` function EventDispatcher() {         this.events = {}; }                         EventDispatcher.prototype.addEventListener = function(type,handler) {         if (typeof handler != 'function') return;         this.events[type] = handler; };                         EventDispatcher.prototype.dispatchEvent = function(type,body) {         var e = {};         e.body = body;         this.events[type](e); };                         var d = new EventDispatcher();                         d.addEventListener('testEvent',function(e){         console.log('callback fired-----' + e.body); });                         d.dispatchEvent('testEvent','213123123'); ```   完毕 收工~~

猜你在找的程序笔记相关文章