标签云

微信群

扫码加入我们

WeChat QR Code

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!")})EDITPosting 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 2This 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 3This 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>


Have you tried using $('body') instead?

2019年04月22日03分52秒

Why would $('body') work instead? Is there a reference somewhere that says to listen on body?

2019年04月22日03分52秒

I don't have any references but each device handles javascript a little differently so iOS might use $('html') a little differently.

2019年04月22日03分52秒

I just looked through the jquery code code.jquery.com/jquery-1.7.1.js and found this in the initialization, if ( selector === "body" && !context && document.body ) {...}. There was nothing similar for the "html" so I'm assuming they would leave that part up to the device instead of manually handling it like they do for the body.

2019年04月22日03分52秒

I'd be surprised if this is a jQuery issue, and the part of the code you linked to seems to just be an optimization for the selector.

1970年01月01日00分03秒

Can you look at my second edit? I tried this with no luck.

2019年04月22日03分52秒

I don't have my iPad in front of me, so I can't check.But I think the target mattered as well.Try my edit above.

2019年04月22日03分52秒

It's possible that this won't work, if you're trying to catch any click on the page.Because of the touch interface, Safari does some weird things to interpret what your finger is actually doing. When I needed to be able to close an element when I clicked away, I used a selector for "not the element shown" instead of body, and that was working fine.

2019年04月22日03分52秒

No dice. I tried $('html').on('click', 'body', ...) as well to no avail (worked in desktop so I think I did it right). Is there somewhere I can go read about these iOS issues?

2019年04月22日03分52秒

the not element shown idea worked!

2019年04月22日03分52秒

See my edit, just tried both of these suggestions.

2019年04月22日03分52秒