简体   繁体   English

在元素单击事件之前触发主体单击事件

[英]body click event triggering before element click event

Here as in image showing the click events bound to a particular element.此处为显示绑定到特定元素的单击事件的图像。

在此处输入图片说明

The a.interstitial handler is attached using jQuery. a.interstitial 处理程序使用 jQuery 附加。 The body handler is attached using addEventListener() (I think, that code is obfuscated and hard to read).主体处理程序使用 addEventListener() 附加(我认为,该代码被混淆且难以阅读)。

I would like to cancel the "body" event handler execution in my "a.interstitial" handler, but I can't because for some reason the body handler always runs first.我想取消“a.interstitial”处理程序中的“body”事件处理程序执行,但我不能,因为出于某种原因,主体处理程序总是首先运行。 This is contrary to my understanding of how event bubbling works.这与我对事件冒泡如何工作的理解相反。 Shouldn't the more specific ("a.interstitial") handler be run before the body handler?更具体的(“a.interstitial”)处理程序不应该在主体处理程序之前运行吗?

As you mentioned body is registered through addEventListener.正如您提到的,body 是通过 addEventListener 注册的。 Can you check in addEventListener call if the third argument is set to true.如果第三个参数设置为 true,您能否检查 addEventListener 调用。 If so it will be dispatched to the registered listener before being dispatched to any EventTarget如果是这样,它将在分派到任何 EventTarget 之前分派到注册的侦听器

Refer https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener参考https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener

In this case, body function gets executed first then the click action of anchor takes place.在这种情况下,首先执行 body 函数,然后发生锚点的点击动作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM