ios javascript html,jquery - iOS bind javascript function to clicking on <html> - Stack Overflow

本文探讨了在iOS Safari中使用JavaScript绑定html元素点击事件的方法。作者尝试了多种方式包括直接绑定到$('html')和使用.delegate()方法,但都无法在iOS设备上触发预期的alert弹窗。最终发现通过选择器$(':not [#stuff]')并使用.on('click')方法成功实现了功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

$(document).ready(function(){

$('html, body').bind("click", function(){alert("html or body")})

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

});

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.

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

EDIT 3

This works!

applesauce
other stuff

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值