1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- <!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>
- div {
- margin-top: 20px;
- }
- #box1 {
- width: 600px;
- height: 600px;
- background-color: red;
- }
- #box2 {
- width: 400px;
- height: 400px;
- background-color: green;
- }
- #box3 {
- width: 200px;
- height: 200px;
- background-color: blue;
- }
- </style>
- </head>
- <body>
- <!--
- 事件:
- 事件捕获 由外向内
- 目标阶段
- 事件冒泡 由内向外
- -->
- <div id="box1">
- <div id="box2">
- <div id="box3"></div>
- </div>
- </div>
- <script>
- var box1 = document.getElementById("box1");
- var box2 = document.getElementById("box2");
- var box3 = document.getElementById("box3");
- // box1.onclick = function(event) {
- // console.log(event)
- // // 取消冒泡事件方法
- // event.stopPropagation();
- // alert("box1")
- // }
- // box2.onclick = function(event) {
- // alert("box2")
- // // IE浏览器
- // event.cancelBubble = true
- // // event.stopPropagation();
- // }
- // box3.onclick = function(event) {
- // alert("box3")
- // event.stopPropagation();
- // }
- // 添加监听事件
- // xxx.addEventListener(事件类型,执行的函数,开启事件类别<true 事件捕获/false 事件冒泡>)
- // xxx.removeEventListener...
- box1.addEventListener('click',function(){
- console.log("1111")
- },false)
- box2.addEventListener('click',function(){
- console.log("222")
- event.stopPropagation();
- },false)
- box3.addEventListener('click',function(){
- console.log("333")
- event.stopPropagation();
- },false)
- </script>
- </body>
- </html>
|