jquery:event.stopImmediatePropagation()vs返回false [英] jquery: event.stopImmediatePropagation() vs return false

查看:86
本文介绍了jquery:event.stopImmediatePropagation()vs返回false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在事件处理程序内调用 event.stopImmediatePropagation() return false 之间有什么区别吗?



$ b

解决方案

false 基本上与调用两者相同, event.stopPropagation()

event.stopImmediatePropagation() event.stopPropagation() 相同,从而阻止执行相同元素的其他注册事件处理程序。因此,它不会阻止事件的默认操作,例如点击链接。



简而言之:

  stop |预防|防止同一元素
bubbling |默认动作|事件处理程序

返回false是是否
preventDefault否是否
stopPropagation是否否
stopImmediatePropagation是否是

return false 也适用于常规JavaScript事件处理程序 / p>

event.stopPropagation() event.preventDefault()也可以在正常JavaScript事件处理程序(在W3C兼容的浏览器中)工作,而 event.stopImmediatePropagation()是jQuery的扩展( update:显然是 DOM Level 3的一部分事件规范)。



注意: return false 阻止事件冒泡p in normal(非jQuery)事件处理程序(请参阅这个答案)(但仍然防止默认动作)。









Is there any difference between calling event.stopImmediatePropagation() and return false inside an event handler ?

解决方案

Yes they are different.

return false is basically the same as calling both, event.stopPropagation() and event.preventDefault().

Whereas event.stopImmediatePropagation() is the same as event.stopPropagation() plus preventing other registered event handlers on the same element to be executed. So it does not prevent the default action for an event, such as following a clicked link.

In short:

                            stop   |    prevent     | prevent "same element"
                          bubbling | default action | event handlers

return false                 Yes           Yes             No
preventDefault               No            Yes             No
stopPropagation              Yes           No              No
stopImmediatePropagation     Yes           No              Yes

return false also works in "normal" JavaScript event handlers

event.stopPropagation() and event.preventDefault() also work in "normal" JavaScript event handlers (in a W3C compatible browser), whereas event.stopImmediatePropagation() is an extension from jQuery (update: apparently it is part of the DOM Level 3 Events specification).

Note: return false does not prevent the event from bubbling up in "normal" (non-jQuery) event handlers (see this answer)(but still prevents the default action).


Maybe worth reading:

这篇关于jquery:event.stopImmediatePropagation()vs返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆