jQuery click() 事件包罗万象?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3761902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery click() event catch-all?
提问by DaveDev
we're showing a box on the screen that I want to hide when the user clicks anywhere on the screen, including body, anchors, divs, buttons, etc... Is there a selector that can handle this for me? Or is it a case of $('body, a, div, input').click()
?
我们在屏幕上显示一个框,当用户单击屏幕上的任何地方时我想隐藏它,包括正文、锚点、div、按钮等......是否有一个选择器可以为我处理这个问题?或者它是一个案例$('body, a, div, input').click()
?
回答by Nick Craver
You can do it like this:
你可以这样做:
$(document).click(function() {
$("#boxID").hide();
});
Since the click
events will, by default, bubble upto document
, this is a "catch all" approach...if you don't want clicks from insidethe box to close it, add a .stopPropagation()
call on thoseclick
events like this:
由于click
事件将在默认情况下,冒泡到document
,这是一个“赶”的办法......如果你不想从点击里面的复选框以关闭它,添加一个.stopPropagation()
随叫随到的click
像这样的活动:
$("#boxID").click(function(e) {
e.stopPropagation();
});
回答by Nithesh Chandra
You can just bind to the click event of document element. Try it at http://jsfiddle.net/ZqEbY/.
您可以绑定到文档元素的点击事件。在http://jsfiddle.net/ZqEbY/尝试一下。