123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <!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: 500px;
- height: 500px;
- background: #00f;
- }
- #box2{
- width: 340px;
- height: 340px;
- background: #0f0;
- }
- #box3{
- width: 200px;
- height: 200px;
- background: #f00;
- }
- </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() {
- alert("111")
- }
- box2.onclick = function(event) {
- alert("222")
- // 解决事件冒泡
- event.stopPropagation();
-
- }
- box3.onclick = function(event) {
- alert("333")
- // IE解决事件冒泡
- event.cancelBubble = true;
- }
- /***
- * 事件三个阶段 :
- * 捕获 由外到内
- * 目标
- * 冒泡 由内到外
- */
- </script>
- </body>
- </html>
|