W3C/JQuery中stopImmediatePropagation和stopPropagation的區別

來源:互聯網
上載者:User

標籤:stoppropagation   immediatepropagation   阻止javascript事件   

W3C的DOM-Level-3標準中,event事件對象有stopPropagation和stopImmediatePropagation這2個函數。

DOM3中Event標準:http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html。

JQuery我用的是2.1.1和1.10.2,也支援這2個標準的函數。

stopImmediatePropagation :

    阻止事件流中當前節點的和所有後續節點的事件監聽器的執行。即影響當前結點的事件監聽器。

stopPropagation:

    阻止事件流中當前節點的所有後續節點的事件監聽器的執行。即不會影響當前節點(currentTarget)的任何事件監聽。

// 註冊順序,就是代碼的執行順序$("#button").click(function(event){alert(1);});$("#button1").click(function(event){alert("2before");event.stopImmediatePropagation();//event.stopPropagation();alert("2after");});$("#button1").click(function(event){alert(3);});

如果使用的是stopPropagation,執行結果是1-->2before-->2before-->3;

如果使用的是stopImmediatePropagation,執行結果是1-->2before-->2before;

 

關於事件處理函數的執行順序問題,如果有疑問,可以參考我的另一篇部落格。

W3C/JQuery中stopImmediatePropagation和stopPropagation的區別

聯繫我們

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