admin管理员组文章数量:1355540
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
.
本文标签:
版权声明:本文标题:javascript - Strange Click Event Bubble on iPhone safari that it stop before bubbling to document.body - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744041475a2580741.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论