jQuery,方法:单击div之外的任何位置,div消失 [英] Jquery, how: click anywhere outside of the div, the div fades out

查看:75
本文介绍了jQuery,方法:单击div之外的任何位置,div消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Jquery中,我如何做到这一点,如果我有一个div,其中包含不同的元素,一个选择,一个搜索输入等,当我在div之外单击时,在页面上,该div就会消失出来,但我可以单击选择并输入搜索输入,但它不会消失吗?任何帮助表示赞赏. -尼克

In Jquery how would i make it so that if i had a div, with different elements inside of it, a select, a search input, etc, that when i click outside of the div, on the page, the div fades out, but i can click on the select and type in the search input and not have it fade? any help is appreciated. -nick

推荐答案

Code Duck的答案不完整,因为如果您单击DIV本身,则需要使其 not 淡出(仅限外部). ).因此,假设您应该淡出的DIV的ID为菜单".

Code Duck's answer is incomplete, because you'd need to have it not fade out if you click the DIV itself (only outside). So say your DIV that's supposed to fade out has an ID of "menu".

jQuery("#menu").click(function(){ return false; });
jQuery(document).one("click", function() { jQuery("#menu").fadeOut(); });

使用 一个 比绑定/取消绑定更简洁.另外,如果您使用 one ,则这里实际上不需要命名空间事件,因为不需要显式取消绑定文档上的特定单击处理程序.

The use of one is more concise than the bind/unbind. Also namespaced events aren't really needed here if you use one because there's no need to explicitly unbind a particular click handler on document.

返回假 只是说停止将事件一直冒泡到文档中."

The return false is just saying "stop bubbling this event up to the document."

这篇关于jQuery,方法:单击div之外的任何位置,div消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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