Html事件冒泡

原覺得span不一樣於input,事件冒泡會被父級標籤吞噬,寫了個測試事件冒泡的Demo,發現並非想得那樣。另外:event.stopPropagation()以及event.stopImmediatePropagation()並不能阻止span冒泡到a標籤中,而簡單粗暴的return false卻能夠。javascript

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>Bubbling</title>
 5     <style type="text/css">
 6         * {
 7             font-size:30px;
 8         }
 9         div {
10             border: 1px blue solid;
11         }
12         span {
13             border: 1px blue solid;
14         }
15     </style>
16     <script type="text/javascript">
17         function setforeColor(sender) {
18             sender.style.color = "red";
19         }
20 
21         function setbgColor(sender) {
22             sender.style.background = "green";
23             return false;
24         }
25     </script>
26 </head>
27 <body>
28     <div>
29         <span onclick="setforeColor(this)">span tag</span> in div
30     </div>
31     <br>
32     <div>
33         <input type="button" value="Button" onclick="setforeColor(this)"/> in div
34     </div>
35     <br>
36     <a href="https://www.baidu.com" style="text-decoration:none;display:block;">
37         <span onclick="setforeColor(this);return false">span tag</span> in anchor
38     </a>
39     <br>
40     <a href="https://www.baidu.com" style="text-decoration:none;display:block;">
41         <span onclick="setbgColor(this)">span tag</span> in anchor
42     </a>
43 </body>
44 </html>

更多參考:https://en.wikipedia.org/wiki/Event_bubblinghttp://javascript.info/bubbling-and-capturing
css