File tree 1 file changed +56
-36
lines changed
25 - Event Capture, Propagation, Bubbling and Once
1 file changed +56
-36
lines changed Original file line number Diff line number Diff line change 1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
- < head >
4
- < meta charset ="UTF-8 ">
5
- < title > Understanding JavaScript's Capture</ title >
6
- < link rel ="icon " href ="https://fav.farm/🔥 " />
7
- </ head >
8
- < body class ="bod ">
9
-
10
- < div class ="one ">
11
- < div class ="two ">
12
- < div class ="three ">
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < title > Understanding JavaScript's Capture</ title >
6
+ < link rel ="icon " href ="https://fav.farm/🔥 " />
7
+ </ head >
8
+ < body class ="bod ">
9
+ < div class ="one ">
10
+ < div class ="two ">
11
+ < div class ="three "> </ div >
13
12
</ div >
14
13
</ div >
15
- </ div >
16
14
17
- < style >
18
- html {
19
- box-sizing : border-box;
20
- }
15
+ < style >
16
+ html {
17
+ box-sizing : border-box;
18
+ }
21
19
22
- * , * : before , * : after {
23
- box-sizing : inherit;
24
- }
20
+ * ,
21
+ * : before ,
22
+ * : after {
23
+ box-sizing : inherit;
24
+ }
25
25
26
- div {
27
- width : 100% ;
28
- padding : 100px ;
29
- }
26
+ div {
27
+ width : 100% ;
28
+ padding : 100px ;
29
+ }
30
30
31
- .one {
32
- background : thistle;
33
- }
31
+ .one {
32
+ background : thistle;
33
+ }
34
34
35
- .two {
36
- background : mistyrose;
37
- }
35
+ .two {
36
+ background : mistyrose;
37
+ }
38
38
39
- .three {
40
- background : coral;
41
- }
42
- </ style >
39
+ .three {
40
+ background : coral;
41
+ }
42
+ </ style >
43
43
44
- < button > </ button >
45
- < script >
44
+ < button > </ button >
45
+ < script >
46
+ const divs = document . querySelectorAll ( "div" ) ;
47
+ function logText ( e ) {
48
+ console . log ( this . classList . value ) ;
49
+ e . stopPropagation ( ) ;
50
+ }
46
51
47
- </ script >
48
- </ body >
52
+ divs . forEach ( ( div ) =>
53
+ div . addEventListener ( "click" , logText , {
54
+ capture : false ,
55
+ once : true ,
56
+ } )
57
+ ) ;
58
+ button . addEventListener (
59
+ "click" ,
60
+ ( ) => {
61
+ console . log ( "Clickkk" ) ;
62
+ } ,
63
+ {
64
+ once : true ,
65
+ }
66
+ ) ;
67
+ </ script >
68
+ </ body >
49
69
</ html >
You can’t perform that action at this time.
0 commit comments