在 Javascript 事件中,如何确定 stopPropagation() 已被调用?
如果调用e.preventDefault()
,可以在e.defaultPrevented
方法中看到反映。
e.stopPropagation()
是否有类似的属性?如果不是,如何确定?
If e.preventDefault()
is called, can see reflected in e.defaultPrevented
method.
Is there a similar property for e.stopPropagation()
? If not, how to determine?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我没有通过 jQuery 查看他们的方法,但似乎您可以重写基本
Event
对象上的stopPropagation
方法,设置一个标志,然后调用重写的方法方法。像这样的东西:
I haven't looked through jQuery to see their method, but it seems you could override the
stopPropagation
method on the baseEvent
object, set a flag, and then call the overridden method.Something like:
不,没有。
我可以这么说,因为 jQuery 的 isPropagationStopped() 方法在内部没有使用任何浏览器 API,而是手动实现此功能。 (如果浏览器中有这样的功能 - 内置 - jQuery 将使用它而不是手动执行。)
因此,在 jQuery 中,新的事件对象将获得此属性(继承):
然后,当您调用 <在该事件对象上的 code>stopPropagation() 上,jQuery 将手动更改此属性:
returnFalse
和returnTrue
是返回false
的函数和分别为true
。No, there isn't.
I can tell, because jQuery's
isPropagationStopped()
method doesn't use any browser API internally, but instead implements this feature manually. (If there were such a feature in the browsers - built-in - jQuery would use it instead of doing it manually.)So, in jQuery, a new event object will get this property (inherited):
and then, when you invoke
stopPropagation()
on that event object, jQuery will manually alter this property:returnFalse
andreturnTrue
are functions which returnfalse
andtrue
respectively.在 IE 中,您可以检查属性
e.cancelBubble
来检查本机停止。其他浏览器没有此功能。但是,您可以自己跟踪它。由于它的性质,您无法在任何地方检查它,除了在停止传播本身的同一处理程序中(事件不会冒泡,不会调用更多处理程序),所以我可以'无法想象有必要这样做的情况。你想做什么?
编辑:我以为你一开始使用的是 jQuery。对于那些:
您可以使用
e.isPropagationStopped()
方法,该方法将检查 jQuery 是否已停止传播。如果传播本身就停止了,这将无法判断。In IE, you can check the property
e.cancelBubble
to check for native stops. Other browsers do not have this functionality.However, you could just keep track of it yourself. Because of the nature of it, you wouldn't be able to check it anywhere, except in the same handler that stops the propagation itself (the event won't bubble up, more handlers won't get called), so I can't imagine a situation where it would be necessary. What are you trying to do?
Edit: I thought you were using jQuery at first. For those who are:
You can use the
e.isPropagationStopped()
method, which will check if propagation has been stopped from jQuery. If propogation is stopped natively, this won't be able to tell.