jQuery click()事件包含所有内容? [英] jQuery click() event catch-all?

查看:95
本文介绍了jQuery click()事件包含所有内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在屏幕上显示了一个框,当用户单击屏幕上的任意位置(包括主体,锚点,div,按钮等)时,我想隐藏该框.是否有一个选择器可以为我处理?还是$('body, a, div, input').click()的情况?

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()?

推荐答案

您可以这样做:

$(document).click(function() {
  $("#boxID").hide();
});

由于默认情况下click事件将冒泡,这是一种包罗万象"的方法...如果您不希望点击框内的 来关闭它,请添加

Since the click events will, by default, bubble up to document, this is a "catch all" approach...if you don't want clicks from inside the box to close it, add a .stopPropagation() call on those click events like this:

$("#boxID").click(function(e) {
  e.stopPropagation();
});

这篇关于jQuery click()事件包含所有内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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