js中addEventListener中第3個參數

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   strong   

<div id="id1" style="width:200px; height:200px; position:absolute; top:100px; left:100px; background-color:blue; z-index:4">      <div id="id2" style="width:200px; height:200px; position:absolute; top:20px; left:70px; background-color:green; z-index:1"></div>  </div>

 addEventListener中的第三個參 數是useCapture, 一個bool類型。當為false時為冒泡擷取(由裡向外),true為capture方式(由外向裡)。

document.getElementById(‘id1‘).addEventListener(‘click‘, function() { console.log(‘id1‘);}, false);document.getElementById(‘id2‘).addEventListener(‘click‘, function() { console.log(‘id2‘);}, false);

 點擊id2的div結果是: id2, id1

document.getElementById(‘id1‘).addEventListener(‘click‘, function() { console.log(‘id1‘);}, false);document.getElementById(‘id2‘).addEventListener(‘click‘, function() { console.log(‘id2‘);}, true);

 結果是:id1,id2

 

DOM方法 addEventListener() 和 removeEventListener()是用來分配和刪除事件的函數。 這兩個方法都需要三個參數,分別為:

事件名稱(String)、要觸發的事件處理函數(Function)、指定事件處理函數的時期或階段(boolean)。

DOM事件流(剪自javascript進階程式設計):

由圖可知捕獲過程要先於冒泡過程

當第三個參數設定為true就在捕獲過程中執行,反之就在冒泡過程中執行處理函數。

聯繫我們

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