-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventmodel.html
More file actions
69 lines (58 loc) · 1.74 KB
/
eventmodel.html
File metadata and controls
69 lines (58 loc) · 1.74 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>事件模型</title>
</head>
<body>
<div>
<p>点击</p>
</div>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
</ul>
<script>
var p = document.querySelector('p');
var div = document.querySelector('div');
var phases = {
1 : 'capture',
2 : 'target' ,
3 : 'bubble'
};
div.addEventListener('click',callback,true);
p.addEventListener('click',callback,true);
div.addEventListener('click', callback, false);
p.addEventListener('click', callback, false);
function callback(event){
var tag = event.currentTarget.tagName;
var phase = phases[event.eventPhase];
console.log("Tag : '"+ tag +". EventPhase: "+ phase +"'");
}
var ul = document.querySelector('ul');
ul.addEventListener('click',function(event){
if(event.target.tagName.toLowerCase() === 'li'){
console.log(event.target.innerHTML)
}
})
//阻止事件的传播
p.addEventListener('click',function(e){
e.stopPropagation();
},true);
p.addEventListener('click' ,function(e){
e.stopPropagation();
},false);
p.addEventListener('click',function(event){
event.stopImmediatePropagation();
console.log(1);
})
p.addEventListener('click',function(event){
console.log(2);
})
</script>
</body>
</html>