-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path模拟jQuery的事件绑定到触发过程.html
240 lines (186 loc) · 5.77 KB
/
模拟jQuery的事件绑定到触发过程.html
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模拟jQuery的事件绑定到触发过程</title>
</head>
<body>
<div id="A" style="background-color: deeppink">
这是A
<div id="B" style="background-color: aqua">
这是B
</div>
</div>
<script>
//数据缓存
let events={}
function $(elemId){
//只考虑理想情况
const element=document.querySelector(elemId)
// console.log(element,'element27')
function returnTrue(){
return true
}
function returnFalse(){
return false
}
$.event={
//不考虑用户的自定义事件
add:function (elemId,type,selectorReal,callbackReal) {
let elemData=events[elemId]
if(!elemData){
events[elemId]=elemData={}
}
elemData.handle=function(nativeEvent){
//锁定this
return $.event.dispatch.call(this,nativeEvent)
}
if(!elemData[type]){
elemData[type]=[]
elemData[type].delegateCount=0
//addEventListener只绑定一次
document.querySelector(elemId).addEventListener(type,elemData.handle)
}
let handlersCount=elemData[type].length
let handlerObj={
type:type,
handler:callbackReal,
guid:++handlersCount,
selector:selectorReal,
}
if ( selectorReal ) {
//在下标为handlers.delegateCount++的位置插入委托事件
elemData[type].splice( elemData[type].delegateCount++, 0, handlerObj);
} else {
elemData[type].push(handlerObj)
}
},
dispatch:function (nativeEvent,) {
let event=$.event.fix(nativeEvent)
let handlers=events['#'+this.id][event.type]
//继续锁定this
let handlerQueue=$.event.handlers.call(this, event, handlers )
//为什么要用变量代替,因为循环的时候,需要保留该值
let matched,handleObj
let i=0
while((matched=handlerQueue[i++])&&!event.isPropagationStopped()){
let j=0
while((handleObj=matched.handlers[j++])){
event.handleObj=handleObj
handleObj.handler(event)
}
}
// return event
},
fix:function (nativeEvent,) {
let $event={}
//就是MouseEvent
$event.originalEvent=nativeEvent
$event.target=nativeEvent.target
$event.type=nativeEvent.type
// delegateTarget: div#A,
// currentTarget: div#A,
$event.timeStamp=Date.now()
$event.stopPropagation=function() {
this.isPropagationStopped = returnTrue;
nativeEvent.stopPropagation()
}
$event.isPropagationStopped=returnFalse
//fix 的标志
$event['chen'+(new Date()).valueOf()]=true
return $event
},
handlers:function (event,handlers) {
let delegateCount = handlers.delegateCount
let cur=event.target
let handlerQueue=[]
for(;cur!==this;cur=cur.parentNode||this){
let matchedHandlers = []
for(let i=0;i<delegateCount;i++){
let handleObj=handlers[i]
matchedHandlers.push( handleObj )
handlerQueue.push( { elem: cur, handlers: matchedHandlers } )
}
}
cur=this
if ( delegateCount < handlers.length ) {
handlerQueue.push( { elem: cur, handlers: handlers.slice( delegateCount ) } )
}
return handlerQueue
},
trigger:function (elemId,type) {
let element=document.querySelector(elemId)
let eventPath=[]
let cur=element
let event={}
event.target=cur
event.type=type
for(;cur;cur=cur.parentNode){
eventPath.push( cur );
}
let i=0
//不考虑阻止冒泡的情况
while((cur=eventPath[i++])){
let handle=events['#'+cur.id]&&events['#'+cur.id].handle
if(handle){
handle.call(cur,event)
}
}
},
}
return {
on:function (type,selector,callback) {
let callbackReal,selectorReal
if(!type){
return
}
//如果selector是funcion的话,就没有委托元素了
if(typeof selector==='function'&&!callback){
selectorReal=undefined
callbackReal=selector
}else if(typeof selector==='string'&&callback){
selectorReal=selector
callbackReal=callback
}
return $.event.add(elemId,type,selectorReal,callbackReal)
},
trigger:function (type) {
return $.event.trigger(elemId,type)
},
}
}
//仅支持id选择器,事件冒泡与事件委托
//=========test1===============
$("#A").on("click" ,function (event) {
console.log(event,"A被点击了")
})
$("#A").on("click" ,function (event) {
console.log(event,"A又被点击了")
})
//=========test2===============
// $("#A").on("click" ,function (event) {
// console.log(event,"A被点击了")
// })
// $("#A").on("click" ,"#B",function (event) {
// event.stopPropagation()
// console.log(event,"B委托A被点击了")
// })
//=========test3===============
// $("#A").on("click" ,function (event) {
// console.log(event,"A被点击了")
// })
// $("#B").on("click",function (event) {
// // event.stopPropagation()
// console.log(event,"B被点击了")
// })
//==========test4==============
// $("#A").on("click" ,function (event) {
// console.log(event,"A被点击了")
// })
// $("#A").on("click" ,function (event) {
// console.log(event,"A又被点击了")
// })
// $("#A").trigger("click")
</script>
</body>
</html>