File tree Expand file tree Collapse file tree 1 file changed +48
-24
lines changed
25 - Event Capture, Propagation, Bubbling and Once Expand file tree Collapse file tree 1 file changed +48
-24
lines changed Original file line number Diff line number Diff line change 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 ">
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 : 100 px ;
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 >
You can’t perform that action at this time.
0 commit comments