Javascript / jQuery - 在 iPhone 上的元素外部点击

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10574228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:19:00  来源:igfitidea点击:

Javascript / jQuery - Tap outside an element on an iPhone

javascriptjqueryiphoneios

提问by shrewdbeans

I found a great answer on detecting a click outside a div from this question: How do I detect a click outside an element?, which works fantastically.

我从这个问题中找到了一个关于检测 div 外点击的很好的答案:如何检测元素外的点击?,这非常有效。

But I've noticed that it doesn't work on the iPhone when testing my site, if I tap outside the element.

但是我注意到在测试我的网站时它在 iPhone 上不起作用,如果我点击元素外部。

Here is the code (taken directly from that answer)

这是代码(直接取自该答案)

$('html').click(function() {
    //Hide the menus if visible
});

$('#menucontainer').click(function(event){
    event.stopPropagation();
});

回答by Yannick Schuchmann

this worked for me :)

这对我有用:)

$('html').on('touchstart', function(e) {
    $('.navbar-flyout').hide();
})
$(".navbar-flyout").on('touchstart',function(e) {
    e.stopPropagation();
});

回答by Ugur Catak

var doc = document.documentElement;
doc.addEventListener('click', function (e) { //TODO();});

The trick:

诀窍:

/*Add this line into only mobile css*/
body{cursor:pointer}