最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 10

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论