亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

jquery事件中的stopPropagation和stopImmediatePropagation

jquery事件中的stopPropagation和stopImmediatePropagation

元芳怎么了 2019-03-22 18:19:02
如題,看文檔中說前者是阻止事件冒泡,后者是阻止所有被定義handler響應。那么是否可以這么理解,stopPropagation是在原生的DOM事件中阻止了事件向后傳遞,而stopImmediatePropagation是直接cancel掉了你在jQuery上定義的handler。兩者的區別就是,一個在DOM上stop,一個在jQuery里stop?那么stopPropagation豈不是就把stopImmediatePropagation的功能包括了,還需要設計后者干嘛?
查看完整描述

1 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

請看文檔里對event.stopImmediatePropagation()的描述:

Keeps the rest of the handlers from being executed and prevents the event from bubbling up the DOM tree.

從這里可以看出,stopImmediatePropagation做了兩件事情:
第一件事:阻止 綁定在事件觸發元素的 其他同類事件的callback的運行,看他下面的例子就很明白:

$("p").click(function(event) {

  event.stopImmediatePropagation();

});

$("p").click(function(event) {

  // 不會執行以下代碼

  $(this).css("background-color", "#f00");

});

第二件事,阻止事件傳播到父元素,這跟stopPropagation的作用是一樣的。

所以文檔里面還有這么一句話:

.. this method also stops the bubbling by implicitly calling event.stopPropagation().

意思是說其實這個方法是調用了stopPropagation()方法的。

stopImmediatePropagation比stopPropagation多做了第一件事情,這就是他們之間的區別


查看完整回答
反對 回復 2019-04-11
  • 1 回答
  • 0 關注
  • 321 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號