12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- <!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: 300px;
- height: 300px;
- background: #f00;
- margin-top: 30px;
- }
- #box2 {
- width: 150px;
- height: 150px;
- background: #ff0;
- }
- #box3 {
- width: 300px;
- height: 300px;
- background: #00f;
- margin-top: 30px;
- }
- #box4 {
- width: 150px;
- height: 150px;
- background: #0f0;
- }
- </style>
- </head>
- <body>
- <!--
- 万维网联盟(W3C)
- DOM事件流:三个阶段
- 事件捕获
- 目标阶段
- 事件冒泡
- -->
- <div id="box1">
- <div id="box2"></div>
- </div>
- <div id="box3">
- <div id="box4"></div>
- </div>
- <script>
- var box1 = document.getElementById("box1");
- var box2 = document.getElementById("box2");
- var box3 = document.getElementById("box3");
- var box4 = document.getElementById("box4");
- box1.addEventListener("click",function(){
- console.log(this,'box1')
- },true)
- box2.addEventListener("click",function(){
- console.log(this,'box2')
- },true)
- box3.addEventListener("click",function(){
- console.log(this,'box3')
- },false)
- box4.addEventListener("click",function(){
- console.log(this,'box4')
- },false)
- // removeEventListener('监听的时间方法',执行的函数,触发的事件类型:true (事件捕获) / false(事件冒泡))
- </script>
- </body>
- </html>
|