-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvent Delegation.html
43 lines (43 loc) · 1.1 KB
/
Event Delegation.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Event Delegation Example</title>
<style>
ul {
list-style: none;
padding: 0;
margin: 0;
}
li {
padding: 10px;
border: 1px solid #ccc;
margin: 5px;
cursor: pointer;
}
li:hover {
background-color: aliceblue;
}
</style>
</head>
<body>
<h2>Event Delegation Example</h2>
<ul id="mylist">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
<li>item 4</li>
<li>item 5</li>
<li>item 6</li>
</ul>
<script>
document.getElementById('mylist').addEventListener('click', function(event){
if(event.target && event.target.tagName === 'LI'){
console.log('Clicked on:',event.target.innerText);
event.target.style.color = 'red';
}
});
</script>
</body>
</html>