-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path事件委拖.html
50 lines (44 loc) · 1.21 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
<!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>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<style>
div{
background:red;
}
</style>
</head>
<body>
<ul id="box">
<li>li1</li>
<li>li2</li>
<li>li3</li>
<li>li4</li>
<li>li5</li>
</ul>
<script>
// 事件委拖
// var box = document.getElementById("box");
box.addEventListener("click",function(event){
console.log(this)
var target = event.target;
//防止父元素ul也触发事件
if(target.nodeName == "LI"){
target.style.backgroundColor = "red";
}
})
// 普通事件
// var box = document.getElementById("box");
// var li = box.getElementsByTagName("li");
// for(var i=0;i<li.length;i++){
// li[i].addEventListener('click',function(){
// this.style.backgroundColor='red'
// })
// }
</script>
</body>
</html>