javascript事件冒泡簡單樣本,javascript冒泡

來源:互聯網
上載者:User

javascript事件冒泡簡單樣本,javascript冒泡

本文執行個體講述了javascript事件冒泡的定義與用法。分享給大家供大家參考,具體如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="zh" xml:lang="zh"> <head>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  <meta name="developer" content="Realazy" />  <title>Bubble in JavaScript DOM</title>  <style type="text/css" media="screen">   div * {    display: block;    margin: 4px;    padding: 4px;    border: 1px solid white;   }   textarea {    width: 20em;    height: 2em;   }  </style>  <script type="text/javascript">   //<![CDATA[   function init(){    var log = document.getElementsByTagName('textarea')[0];    var all = document.getElementsByTagName('div')[0].getElementsByTagName('*');    for (var i = 0, n = all.length; i < n; ++i) {     all[i].onmouseover = function(e){      this.style.border = '1px solid red';      log.value = '滑鼠現在進入的是: ' + this.nodeName;     };     all[i].onmouseout = function(e){      this.style.border = '1px solid white';     };    }    var all2 = document.getElementsByTagName('div')[1].getElementsByTagName('*');    for (var i = 0, n = all2.length; i < n; ++i) {     all2[i].onmouseover = function(e){      this.style.border = '1px solid red';      if (e) //停止事件冒泡       e.stopPropagation();      else        window.event.cancelBubble = true;      log.value = '滑鼠現在進入的是: ' + this.nodeName;     };     all2[i].onmouseout = function(e){      this.style.border = '1px solid white';     };    }   }   window.onload = init;   //]]>  </script> </head> <body>  <h1>Bubble in JavaScript DOM</h1>  <p>   DOM樹的結構是:  </p>  <pre><code>UL - LI  - A - SPAN</code></pre>  <div>   <ul>    <li>     <a href="http://www.bkjia.com/#"><span>Bubbllllllllllllllle</span></a>    </li>    <li>     <a href="http://www.bkjia.com/#"><span>Bubbllllllllllllllle</span></a>    </li>   </ul>  </div>  <textarea>  </textarea>  <p>   滑鼠進入UL的任何一個子項目,如果不停止冒泡,我們從UL到SPAN都定義了滑鼠移至上方(   <code>    mouseover   </code>)事件,這個事件會上升了UL,從而從滑鼠所進入的元素到UL元素都會有紅色的邊。  </p>  <div>   <ul>    <li>     <a href="http://www.bkjia.com/#"><span>Bubbllllllllllllllle</span></a>    </li>    <li>     <a href="http://www.bkjia.com/#"><span>Bubbllllllllllllllle</span></a>    </li>   </ul>  </div>  <p>   如果停止冒泡,事件不會上升,我們就可以擷取精確的滑鼠進入元素。  </p> </body></html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.