iOS绑定javascript函数以点击< html> [英] iOS bind javascript function to clicking on <html>

查看:78
本文介绍了iOS绑定javascript函数以点击< html>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这适用于桌面Safari,但在iOS版本中不会弹出警报。是否可以绑定到iOS中的'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.jstype =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.jstype =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.jstype =text / javascript>< / script>
< body>
< div id =stuff>苹果酱< / div>
< div>其他内容< / div>
< / body>
< script type =text / javascript> $(':'['])。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年份中,on(event,selector,function)都是最好的选择。它适用于IOS并抵御泄漏事件处理程序。

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


This works in desktop safari, but in the iOS version no alert pops up. Is it possible to bind to the 'html' element in iOS? I want to close a drop-down menu whenever the user clicks somewhere else on the page.

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

EDIT

Posting this as html since jsfiddle embeds in an iframe, which apparently gets rid of my issue. Opening this page on desktop safari works fine, but in the iOS simulator it doesn't show anything on clicking.

<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

This also doesn't work for me in the iOS simulator or on my iPhone. I do get the alerts if I put the delegate() in my app and click on a link.

<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

This works!

<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() doesn't function properly on iOS. I suspect that you're having the same problem with bind(). jQuery.delegate() however does work.

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

Should work for you. Presumably on() will also function, but I haven't had a chance to try it yet.

As an aside, you should move that script block below the body tag and lose the $(document).ready().

Update: In any recent vintage of jQuery, on(event, selector, function) is the way to go. It works on IOS and resists leaking event handlers.

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

这篇关于iOS绑定javascript函数以点击&lt; html&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆