File tree Expand file tree Collapse file tree 1 file changed +64
-45
lines changed
25 - Event Capture, Propagation, Bubbling and Once Expand file tree Collapse file tree 1 file changed +64
-45
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 ">
12- </ div >
13- </ div >
14- </ div >
15-
16- < style >
17- html {
18- box-sizing : border-box;
19- }
20-
21- * , * : before , * : after {
22- box-sizing : inherit;
23- }
24-
25- div {
26- width : 100% ;
27- padding : 100px ;
28- }
29-
30- .one {
31- background : thistle;
32- }
33-
34- .two {
35- background : mistyrose;
36- }
37-
38- .three {
39- background : coral;
40- }
41- </ style >
42-
43- < button > </ button >
44- < script >
45-
46- </ script >
47- </ body >
3+ < head >
4+ < meta charset ="UTF-8 " />
5+ < title > Understanding JavaScript's Capture</ title >
6+ </ head >
7+ < body class ="bod ">
8+ < div class ="one ">
9+ < div class ="two ">
10+ < div class ="three "> </ div >
11+ </ div >
12+ </ div >
13+
14+ < style >
15+ html {
16+ box-sizing : border-box;
17+ }
18+
19+ * ,
20+ * : before ,
21+ * : after {
22+ box-sizing : inherit;
23+ }
24+
25+ div {
26+ width : 100% ;
27+ padding : 100px ;
28+ }
29+
30+ .one {
31+ background : thistle;
32+ }
33+
34+ .two {
35+ background : mistyrose;
36+ }
37+
38+ .three {
39+ background : coral;
40+ }
41+ </ style >
42+
43+ < button > button</ button >
44+ < script >
45+ const divs = document . querySelectorAll ( "div" ) ;
46+ const btn = document . querySelector ( "button" ) ;
47+
48+ function logText ( e ) {
49+ console . log ( this . classList . value ) ;
50+ }
51+
52+ // btn event listener
53+ btn . addEventListener ( "click" , logText , { once : true } ) ;
54+ // event listeners on all the divs
55+ divs . forEach ( ( div ) => {
56+ /* default mode is bubble up from e.target */
57+ // div.addEventListener("click", logText, { capture: false });
58+
59+ /* `{capture: true}` zooms in to e.target */
60+ // div.addEventListener("click", logText, { capture: true });
61+
62+ /* `{once: true}` disables EL after first firing */
63+ div . addEventListener ( "click" , logText , { once : true } ) ;
64+ } ) ;
65+ </ script >
66+ </ body >
4867</ html >
You can’t perform that action at this time.
0 commit comments