简体   繁体   中英

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. The body handler is attached using addEventListener() (I think, that code is obfuscated and hard to read).

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. 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?

As you mentioned body is registered through addEventListener. Can you check in addEventListener call if the third argument is set to true. If so it will be dispatched to the registered listener before being dispatched to any EventTarget

Refer 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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