1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <style>
- .box1{
- width: 400px;
- height: 400px;
- background-color: red;
- }
- .box2{
- width: 200px;
- height: 200px;
- background-color: blue;
- }
- </style>
- </head>
- <body>
- <div class="box1">
- <div class="box2"></div>
- </div>
- <script>
- var oBox1 = document.getElementsByClassName("box1")[0];
- var oBox2 = document.getElementsByClassName("box2")[0];
- // 事件冒泡
- // 事件冒泡:事件从最当前触发事件的元素开始,然后逐步向上冒泡到祖先元素,直到到达文档的根元素(html标签)。
- // 事件冒泡的过程中,事件处理函数会按照元素的层级关系,从内到外依次执行。
- // oBox1.onclick = function(){
- // console.log("box1")
- // }
- // oBox2.onclick = function(){
- // console.log("box2")
- // }
- oBox1.addEventListener("click",function(){
- console.log("3-box1")
- },false)
- oBox2.addEventListener("click",function(){
- console.log("4-box2")
- },false)
- oBox1.addEventListener("click",function(){
- console.log("1-box1")
- },true)
- oBox2.addEventListener("click",function(){
- console.log("2-box2")
- },true)
- </script>
- </body>
- </html>
|