js-addEventListener()第三個參數useCapture

來源:互聯網
上載者:User

標籤:style   blog   color   使用   io   ar   cti   div   

概述:

  第3個參數叫做useCapture,是一個boolean值,就是true or false 。如果送出true的話就是瀏覽器會使用Capture方式,false的話是Bubbling,只有在特定狀況下才會有影響,通常建議是false,而會有影響的情形是目標元素(target element)有祖先元素(ancestor element),而且也有同樣的事件對應函數

html片段

    <div id="div1" style="background: blue;width: 100px; height: 100px;">        <div id="div2" style="background: red;width: 70px; height: 70px;">            <div id="div3" style="background: yellow;width: 50px; height: 50px;"></div>        </div>    </div>

js代碼

var oDvi1 = document.getElementById(‘div1‘),    oDvi2 = document.getElementById(‘div2‘),    oDvi3 = document.getElementById(‘div3‘);// 123 -> 456 -> 345oDvi1.addEventListener(‘click‘, xx1, true);oDvi2.addEventListener(‘click‘, xx2, false);oDvi3.addEventListener(‘click‘, xx3, true);function xx1(){ //藍    alert(123);}function xx2(){ //紅    alert(345);}function xx3(){//黃    alert(456);}

總結:

  在div3上觸發點擊事件

  捕獲階段: 外-》裡 * 在div1處檢測 useCapture 是否為true,是則執行程式, div2同理 .

  目標階段: 目標到div3處,發現div3就是滑鼠點擊的節點, 所以這裡是目標階段。若有事件處理常式,則執行該程式,這裡不論 useCapture 為 true 還是 false。

  冒泡階段: 裡-》外  在div2處檢測useCapture 是否為false, 是則執行該程式 . div1同理 

聯繫我們

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