iOS绑定javascript函数来点击<html>

这适用于桌面Safari,但在iOS版本中不会popup警报。 有没有可能绑定到iOS中的'html'元素? 我想closures一个下拉菜单,只要用户点击页面上的其他地方。

$('html').bind("click", function(){alert("clicked!")}) 

编辑

将其作为html发布,因为jsfiddleembedded了一个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> 

编辑2

这也不适用于我在iOS模拟器或我的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> 

jQuery.click()在iOS上无法正常工作。 我怀疑你有bind()相同的问题。 jQuery.delegate()然而工作。

 $('html').delegate('body', 'click', function(){ alert("body"); }); 

应该为你工作。 大概在()上也会起作用,但是我还没有机会尝试它。

另外,您应该将该脚本块移到body标签下方,并丢失$(document).ready()。

更新:在任何最近的jQuery年份,(事件,select器,函数)是要走的路。 它在IOS上工作,并抵制泄漏事件处理程序。

 $('body').on('click', ':not[#stuff]', function(){ alert("not stuff"); }); 

使用documentbody元素而不是html

  $(document).bind("click", function(){ alert("clicked!") }); 

你也可以试试这个。

  $('html, body').bind("click", function(){ alert("clicked!") });