简体   繁体   中英

Strange Click Event Bubble on iPhone safari that it stop before bubbling to document.body

I have bind a click event as document.body.onclick = function(){alert("aaa")};

It do good both on android or chrome on IOS whatever element I click. But it does not trigger on iPhone safari while click the elements except a and img element.

But it can bubble to body while binding the touchstart event.

At last, I have to add to div(#main) to contain all my elements, and then bind the delegate object on this div.

document.querySelector("#main").onclick = function(){alert("aaa")}

So I am wandering why the onclick event stops before it bubble to body.

I know this is quite old, but I still managed to dig it up when I ran into the very same issue.

In short, mouse events in iOS Safari do not bubble up to the document.body unless iOS Safari thinks the event target is a clickable element.

Clickable element is one of the following:

  1. The target element of the event is a link or a form field.

  2. The target element, or any of its ancestors up to but not including the <body> , has an explicit event handler set for any of the mouse events. This event handler may be an empty function.

  3. The target element, or any of its ancestors up to and including the document has a cursor: pointer CSS declarations.

(Quoted from here , emphasis mine.)

Possible fixes include:

  • Add cursor: pointer to any element that does not fit the description.
  • Add a no-op event handler to the target.
  • Handle the clicks one level below document.body .

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