具体代码如下所示:
- $('body').click(function(e) {
- var target = $(e.target);
- // 如果#overlay或者#btn下面还有子元素,可使用
- // !target.is('#btn *') && !target.is('#overlay *')
- if (!target.is('#btn') && !target.is('#overlay')) {
- if ($('#overlay').is(':visible')) {
- $('#overlay').hide();
- }
- }
- });
或者
- $('body').click(function(e) {
- if(e.target.id != 'btn' && e.target.id != 'overlay')
- if ( $('#overlay').is(':visible') ) {
- $('#overlay').hide();
- }
- })
PS:下面在接着看一段代码jquery 点击除本身外其他地方隐藏
- $("#test").click(function(e) {
- e?e.stopPropagation():event.cancelBubble = true;
- });
- $(document).click(function() {
- $("#test").fadeOut();
- <pre name="code" class="html">e?e.stopPropagation():event.cancelBubble = true; 为阻止冒泡事件</pre> });
- <pre></pre>
- <br>
- <link rel="stylesheet" href="http://static.blog.csdn.net/public/res-min/markdown_views.CSS?v=2.0">
总结
以上所述是小编给大家介绍的jQuery除指定区域外点击任何地方隐藏DIV,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
来源: http://www.jb51.net/article/128108.htm