Skip to content

Commit 9946f35

Browse files
committed
Added Capture, Propagation, Bubbling, & Once (wesbos#25)
1 parent f284ff1 commit 9946f35

File tree

1 file changed

+48
-24
lines changed

1 file changed

+48
-24
lines changed
Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Understanding JavaScript's Capture</title>
67
</head>
8+
79
<body class="bod">
810

911
<div class="one">
@@ -13,33 +15,55 @@
1315
</div>
1416
</div>
1517

16-
<style>
17-
html {
18-
box-sizing: border-box;
19-
}
20-
*, *:before, *:after { box-sizing: inherit; }
18+
<style>
19+
html {
20+
box-sizing: border-box;
21+
}
22+
23+
*,
24+
*:before,
25+
*:after {
26+
box-sizing: inherit;
27+
}
28+
29+
div {
30+
width: 100%;
31+
padding: 100px;
32+
}
33+
34+
.one {
35+
background: thistle;
36+
}
37+
38+
.two {
39+
background: mistyrose;
40+
}
41+
42+
.three {
43+
background: coral;
44+
}
45+
</style>
2146

22-
div {
23-
width:100%;
24-
padding:100px;
25-
}
47+
<button>Click me!</button>
48+
<script>
49+
const divs = document.querySelectorAll('div');
50+
const btn = document.querySelector('button');
2651

27-
.one {
28-
background: thistle;
29-
}
52+
function logText(e) {
53+
console.log(this.classList.value);
54+
e.stopPropagation();
55+
}
3056

31-
.two {
32-
background:mistyrose;
33-
}
3457

35-
.three {
36-
background:coral;
37-
}
38-
</style>
58+
divs.forEach(div => div.addEventListener('click', logText, {
59+
capture: false,
60+
once: true
61+
}));
3962

40-
<button></button>
41-
<script>
42-
43-
</script>
63+
btn.addEventListener('click', () => console.log('You clicked the button!'), {
64+
once: true
65+
});
66+
</script>
4467
</body>
45-
</html>
68+
69+
</html>

0 commit comments

Comments
 (0)