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 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.
Share Improve this question asked Aug 30, 2013 at 2:53 Jayce LinJayce Lin 4471 gold badge6 silver badges10 bronze badges1 Answer
Reset to default 10I 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:
The target element of the event is a link or a form field.
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.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
.