这在桌面 safari 中有效,但在 iOS 版本中不会弹出警报。是否可以绑定到 iOS 中的“html”元素?每当用户单击页面上的其他位置时,我想关闭下拉菜单。
$('html').bind("click", function(){alert("clicked!")})
EDIT
由于 jsfiddle 嵌入在 iframe 中,因此将其作为 html 发布,这显然解决了我的问题。在桌面 safari 上打开此页面工作正常,但在 iOS 模拟器中单击时不会显示任何内容。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('html, body').bind("click", function(){alert("html or body")})
$(document).bind("click", function(){alert("document")})
});
</script>
<body>
</body>
</html>
EDIT 2
这在 iOS 模拟器或 iPhone 上也不起作用。如果我将 delegate() 放入我的应用程序中并单击链接,我确实会收到警报。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<body>
</body>
<script type="text/javascript">
$(document).delegate('html, body', "click", function(){alert("html or body")});
</script>
</html>
EDIT 3
这有效!
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<body>
<div id="stuff">applesauce</div>
<div>other stuff</div>
</body>
<script type="text/javascript">
$(':not[#stuff]').on( "click", function(){alert("not stuff")});
</script>
</html>