File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed 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+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
6+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7+ < title > Event Bubbling, Capturing and Propagation</ title >
8+ </ head >
9+ < body >
10+ < div id ="output "> </ div >
11+ < script >
12+ const output = document . getElementById ( "output" ) ;
13+
14+ output . innerHTML = `
15+ <h1 id="title"> Event Bubbling, Capturing and Propagation</h1>
16+ <div class="one">
17+
18+ <div class="two">
19+ <p>dddd</p>
20+ <button type="button" id="btn" class="three">Click me plz</button>
21+ </div>
22+ </div>
23+
24+
25+ ` ;
26+ const one = document . querySelector ( ".one" ) ;
27+ const two = document . querySelector ( ".two" ) ;
28+ const three = document . querySelector ( "#btn" ) ;
29+
30+ function handleClick ( e ) {
31+ //stop event bubbling (bottom to top)
32+ e . stopPropagation ( ) ;
33+
34+ e . stopImmediatePropagation ( ) ;
35+ console . log ( e . target ) ;
36+ }
37+ one . addEventListener ( "click" , handleClick ) ;
38+ two . addEventListener ( "click" , handleClick ) ;
39+ three . addEventListener ( "click" , handleClick ) ;
40+
41+ three . addEventListener ( "click" , ( ) => {
42+ console . log ( "twice" ) ;
43+ } ) ;
44+ </ script >
45+ </ body >
46+ </ html >
You can’t perform that action at this time.
0 commit comments