-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path事件委托.html
59 lines (48 loc) · 1.33 KB
/
事件委托.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>时间委托</title>
</head>
<body>
<ul id="parent-list">
<li id="post-1">item 1</li>
<li id="post-2">item 2</li>
<li id="post-3">item 3</li>
<li id="post-4">item 4</li>
</ul>
<div id="myDiv">
<a href="javascript:;" class="classA">123</a>
<a href="javascript:;" class="classA">53215</a>
<a href="javascript:;" class="classA">45456</a>
<p class="classA">dfafdf</p>
</div>
<script>
//找到父元素 添加监听器
document.getElementById('parent-list').addEventListener('click',function(e){
// if(e.target)
// console.log(e.target.id);
if(e.target && e.target.nodeName == 'LI'){
// console.log(1);
console.log("List item",e.target.id.replace("post-",''),"was clicked!");
}
});
//获取父元素myDiv,添加监听器
document.getElementById('myDiv').addEventListener('click',function(e){
// console.log(e.target.className);
if(e.target && e.target.nodeName == 'A'){
var classes = e.target.className.split(" ");
// console.log(classes);
if(classes){
for(var x = 0,len = classes.length;x<len;x++){
if(classes[x] == "classA"){
console.log("是指定元素");
// do something
}
}
}
}
});
</script>
</body>
</html>