编程语言
首页 > 编程语言> > javascript – 奇怪的点击iPhone Safari游戏中的事件泡泡,它在冒泡到document.body之前停止

javascript – 奇怪的点击iPhone Safari游戏中的事件泡泡,它在冒泡到document.body之前停止

作者:互联网

我将click事件绑定为document.body.onclick = function(){alert(“aaa”)};

无论我点击什么元素,它都可以在IOS上的android或chrome上做得很好.
但是在点击除a和img元素之外的元素时,它不会触发iPhone Safari.

但它可以在绑定touchstart事件时冒泡到正文.

最后,我必须添加到div(#main)以包含我的所有元素,然后绑定此div上的委托对象.

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

所以我徘徊为什么onclick事件在它冒泡到身体之前就停止了.

解决方法:

我知道这已经很老了,但是当我遇到同样的问题时,我仍然设法把它挖出来.

简而言之,除非iOS Safari认为事件目标是可点击元素,否则iOS Safari中的鼠标事件不会冒泡到document.body.

可点击元素是以下之一:

  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.

(引自here,强调我的.)

可能的修复包括:

>添加游标:指向任何不符合描述的元素的指针.
>向目标添加无操作事件处理程序.
>处理document.body下一级的点击.

标签:javascript,html5,mobile-safari,mobile-website
来源: https://codeday.me/bug/20190823/1695883.html