File tree Expand file tree Collapse file tree 1 file changed +46
-33
lines changed
25 - Event Capture, Propagation, Bubbling and Once Expand file tree Collapse file tree 1 file changed +46
-33
lines changed Original file line number Diff line number Diff line change 11<!DOCTYPE html>
22< html lang ="en ">
3- < head >
4- < meta charset ="UTF-8 ">
5- < title > Understanding JavaScript's Capture</ title >
6- </ head >
7- < body class ="bod ">
8-
9- < div class ="one ">
10- < div class ="two ">
11- < div class ="three ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Understanding JavaScript's Capture</ title >
6+ </ head >
7+
8+ < body class ="bod ">
9+
10+ < div class ="one ">
11+ < div class ="two ">
12+ < div class ="three ">
13+ </ div >
1214 </ div >
1315 </ div >
14- </ div >
1516
16- < style >
17- html {
18- box-sizing : border-box;
19- }
20- * , * : before , * : after { box-sizing : inherit; }
17+ < style >
18+ html {
19+ box-sizing : border-box;
20+ }
21+ * , * : before , * : after { box-sizing : inherit; }
22+
23+ div {
24+ width : 100% ;
25+ padding : 100px ;
26+ }
27+
28+ .one {
29+ background : thistle;
30+ }
31+
32+ .two {
33+ background : mistyrose;
34+ }
2135
22- div {
23- width : 100 % ;
24- padding : 100 px ;
25- }
36+ . three {
37+ background : coral ;
38+ }
39+ </ style >
2640
27- .one {
28- background : thistle;
29- }
41+ < button > </ button >
3042
31- .two {
32- background : mistyrose;
33- }
43+ < script >
44+ const divs = document . querySelectorAll ( 'div' )
3445
35- . three {
36- background : coral;
37- }
38- </ style >
46+ function logText ( e ) {
47+ console . log ( this . classList . value )
48+ // e.stopPropagation() // stop bubbling
49+ }
3950
40- < button > </ button >
41- < script >
51+ divs . forEach ( div => div . addEventListener ( 'click' , logText , {
52+ capture : false ,
53+ once : true
54+ } ) )
55+ </ script >
4256
43- </ script >
44- </ body >
57+ </ body >
4558</ html >
You can’t perform that action at this time.
0 commit comments