简体   繁体   English

单击附加到 document.body 的事件侦听器不会在 iOS6 中触发?

[英]click event listeners attached to document.body dont fire in iOS6?

Experiencing strange behaviour and non functioning event delegation in iOS6, the root of which is that if I simply attach an event handler to document.body, as so:在 iOS6 中遇到奇怪的行为和无效的事件委托,其根源是如果我简单地将事件处理程序附加到 document.body,如下所示:

document.body.addEventListener("click", function(){alert("ios6 sucks")}, false);

this doesnt get run, for example if I go to google.com and add that via safari remote webinspector.这不会运行,例如,如果我去 google.com 并通过 safari 远程 webinspector 添加它。 In some cases it will run, including if I click on a link on the page, or if clicking on an element that has an event handler attached directly to it.在某些情况下,它会运行,包括如果我单击页面上的链接,或者如果单击一个直接附加了事件处理程序的元素。 The same works fine in major browsers and on iOS 5 and 4. Adding touchend to body will trigger as it should and could be a potential workaround but it is really preferable to let the browser detect clicks instead of having to program some click detection in touchstart/touchend.在主要浏览器和 iOS 5 和 4 上同样可以正常工作。将 touchend 添加到 body 将触发它应该并且可能是一个潜在的解决方法,但是让浏览器检测点击而不是必须在 touchstart 中编写一些点击检测程序确实更可取/触摸结束。 Am wondering if this is an iOS6 bug.我想知道这是否是 iOS6 的错误。 but I havent seen anyone else complain about this yet.但我还没有看到其他人抱怨过这个。

@Bonkers... Referencing why the body click gets triggered after attaching the click event to the div is solely due to event bubbling. @Bonkers ...引用为什么在将点击事件附加到div之后触发身体点击完全是由于事件冒泡。

I replicated your code here showing that it gets called twice on the div but only once on the body:我在这里复制了你的代码,表明它在 div 上被调用了两次,但在 body 上只调用了一次:

<div id="myDiv">FlackAttack Test</div>

<script>
document.body.addEventListener("click", function(){alert("ios6 sucks")}, false);
document.getElementById('myDiv').addEventListener('click', function(){alert("ios6 sucks twice")},      false);
</script>

If you wanted to stop the event from bubbling, you could call e.stopPropagation();如果你想阻止事件冒泡,你可以调用 e.stopPropagation(); or e.cancelBubble = true;或者 e.cancelBubble = true;

Faced the same problem.面临同样的问题。 The following helped (omitting.body):以下帮助(省略.body):

document.addEventListener("click", function(){alert("ios6 sucks")}, false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM