這適用於桌面Safari,但在iOS版本中不會彈出警報。是否可以綁定到iOS中的'html'元素?我想在用戶點擊頁面上的其他位置時關閉下拉菜單。iOS綁定javascript功能點擊<html>
$('html').bind("click", function(){alert("clicked!")})
編輯
發佈此爲HTML,因爲的jsfiddle在iframe中,這顯然擺脫了我的問題的嵌入。在桌面上打開此頁面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
這也不會在iPhone模擬器或在我的iPhone爲我工作。如果我將委託()放入我的應用程序並單擊鏈接,我確實會收到警報。
<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>
編輯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>
您是否嘗試過使用$('body')來代替? – 2012-02-08 19:42:05
爲什麼$('body')會改爲工作?有沒有一個參考文獻說要聽身體? – spike 2012-02-08 20:26:28
我沒有任何引用,但每個設備處理JavaScript有點不同,所以iOS可能會使用$('html')稍有不同。 – 2012-02-08 20:30:43