js的捕捉事件,冒泡事件

來源:互聯網
上載者:User

標籤:

冒泡事件可以查詢上個隨筆,

捕捉事件正好和冒泡時間正反著 

所以這代碼我把冒泡事件注釋,

html和css的內容

 

<style type="text/css">    #box1{width:500px;height:500px;background:#900;}     #box2{width:400px;height:400px;background:#090;}      #box3{width:300px;height:300px;background:#009;}       #box4{width:200px;height:200px;background:#990;}        #box5{width:100px;height:100px;background:#099;}</style><body><div id="box1">    <div id="box2">       <div id="box3">                <div id="box4">                       <div id="box5">                           </div>                </div>       </div>    </div></div>

 

js部分代碼

 1 <script> 2     window.onload=function() 3     { 4       //監聽事件 5         var $=function(_id){return document.getElementById(_id);} 6         document.addEventListener("click", function(){alert("捕抓中,你點擊了document")}, true); 7         document.body.addEventListener("click", function(){alert("捕抓中,你點擊了body")}, true); 8            $("box1").addEventListener("click", function(){alert("捕抓中,你點擊了id為:"+this.id +"的div")}, true); 9               $("box2").addEventListener("click", function(){alert("捕抓中,你點擊了id為:"+this.id +"的div")}, true);10                  $("box3").addEventListener("click", function(){alert("捕抓中,你點擊了id為:"+this.id +"的div")}, true);11                      $("box4").addEventListener("click", function(){alert("捕抓中,你點擊了id為:"+this.id +"的div")}, true);12                         // $("box5").addEventListener("click", function(){alert("捕抓中,你點擊了id為:"+this.id +"的div")}, true);13   14 15      //冒泡事件16      // var $=function(_id){return document.getElementById(_id);}17         // document.addEventListener("click", function(){alert("冒泡中,你點擊了document")}, false);18         // document.body.addEventListener("click", function(){alert("冒泡中,你點擊了body")}, false);19         //    $("box1").addEventListener("click", function(){alert("冒泡中,你點擊了id為:"+this.id +"的div")}, false);20         //       $("box2").addEventListener("click", function(){alert("冒泡中,你點擊了id為:"+this.id +"的div")}, false);21         //          $("box3").addEventListener("click", function(){alert("冒泡中,你點擊了id為:"+this.id +"的div")}, false);22         //              $("box4").addEventListener("click", function(){alert("冒泡中,你點擊了id為:"+this.id +"的div")}, false);23         //                 // $("box5").addEventListener("click", function(){alert("冒泡中,你點擊了id為:"+this.id +"的div")}, false);24 25 26 //刪除監聽事件,必須完全一樣27 var del =function(){alert("冒泡中,你點擊了id為: "+this.id +"的div")}28  $("box5").addEventListener("click", del, false);29  // $("box5").removeEventListener("click",del, false);30 31 32 33  //ie用的是attachEvent()  和detachEvent()  都有兩個參數   ie11也不支援了34  // $("box5").attachEvent("onclick",del);35  // $("box5").detachEvent("onclick",del);36 37 38     }39 </script>

其實捕捉監聽事件和冒泡事件只是函數的一個參數不同,一個true,一個false。

其實主要有時候我們實現某些功能時,需要移除捕捉和冒泡事件。

 

 

  

 

js的捕捉事件,冒泡事件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.