File tree Expand file tree Collapse file tree
25 - Event Capture, Propagation, Bubbling and Once Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < 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 class ="button "> </ button >
44+ < script >
45+ let divs = document . querySelectorAll ( 'div' ) ;
46+ let button = document . querySelector ( 'button' ) ;
47+ divs . forEach ( div => div . addEventListener ( 'click' , logText , {
48+ once : false ,
49+ capture : true
50+ } )
51+ ) ;
52+
53+ button . addEventListener ( 'click' , logText , {
54+ once : true
55+ } ) ;
56+
57+ function logText ( e ) {
58+ console . log ( this . classList . value ) ;
59+ e . stopPropagation ( ) ;
60+ }
61+ </ script >
62+ </ body >
63+ </ html >
You can’t perform that action at this time.
0 commit comments