如何在元素之外的任何位置隐藏单击事件中的元素?
我想知道这是否是单击页面上任意位置时隐藏可见元素的正确方法。
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
当元素边界内发生单击事件时,元素(div、span 等)不应消失。
I would like to know whether this is the correct way of hiding visible elements when clicked anywhere on the page.
$(document).click(function (event) {
$('#myDIV:visible').hide();
});
The element (div, span, etc.) shouldn't disappear when a click event occurs within the boundaries of the element.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
如果我理解,当您单击除 div 之外的任何位置时,您想隐藏该 div,并且如果您在 div 上单击,则它不应该关闭。 您可以使用以下代码来做到这一点:
这会将点击绑定到整个页面,但如果您点击相关的 div,它将取消点击事件。
If I understand, you want to hide a div when you click anywhere but the div, and if you do click while over the div, then it should NOT close. You can do that with this code:
This binds the click to the entire page, but if you click on the div in question, it will cancel the click event.
试试这个,
我使用类名而不是ID,因为在asp.net中你必须担心.net附加到id的额外内容
编辑-
由于您添加了另一块,它的工作原理如下:
Try this
I use class name instead of ID, because in asp.net you have to worry about the extra stuff .net attaches to the id
EDIT-
Since you added a another piece, it would work like this:
从 jQuery 1.7 开始,有一种新的方法来处理事件。 我想我在这里回答只是为了演示我如何以“新”方式做到这一点。 如果您还没有,我建议您阅读“on”方法的 jQuery 文档。
这里我们滥用了 jQuery 的选择器和事件冒泡。 请注意,我确保事后清理事件处理程序。 您可以使用
$('.container').one
自动执行此行为(请参阅:文档)但因为我们需要在处理程序中执行条件,所以这里不适用。As of jQuery 1.7 there's a new way to handle events. I thought I'd answer here just to demonstrate how I might go about doing this the "new" way. If you haven't, I recommend you read the jQuery docs for the "on" method.
Here we're abusing jQuery's selectors and bubbling of events. Note that I make sure I clean the event handler up afterwards. You can automate this behaviour with
$('.container').one
(see: docs) but because we need to do conditionals within the handler that isn't applicable here.下面的代码示例似乎最适合我。 虽然您可以使用“return false”来停止 div 或其任何子级对该事件的所有处理。 如果您想在弹出 div 上进行控件(例如弹出登录表单),您需要使用 event.stopPropogation()。
This following code example seems to work best for me. While you can use 'return false' that stops all handling of that event for the div or any of it's children. If you want to have controls on the pop-up div (a pop-up login form for example) you need to use event.stopPropogation().
谢谢,托马斯。 我是 JS 的新手,我一直在疯狂地寻找解决我的问题的方法。 你的有帮助。
我使用 jquery 制作了一个向下滑动的登录框。 为了获得最佳的用户体验,我决定当用户单击该框以外的其他位置时使该框消失。 我对花了大约四个小时来解决这个问题感到有点尴尬。 但是嘿,我是 JS 新手。
也许我的代码可以帮助别人:
Thanks, Thomas. I'm new to JS and I've been looking crazy for a solution to my problem. Yours helped.
I've used jquery to make a Login-box that slides down. For best user experience I desided to make the box disappear when user clicks somewhere but the box. I'm a little bit embarrassed over using about four hours fixing this. But hey, I'm new to JS.
Maybe my code can help someone out:
您还可以做的是:
如果您的目标不是 div,则通过检查其长度是否为零来隐藏 div。
What you can also do is:
If your target is not a div then hide the div by checking its length is equal to zero.
我做了下面的事情。 想到分享,这样其他人也能受益。
如果我可以在这方面帮助别人,请告诉我。
I did the below. Thought of sharing so someone else could also benefit.
Let me know if I can help someone on this.
这里 #suggest_input 是文本框的名称,.suggest_container 是 ul 类名称,.suggest_div 是我的自动建议的主要 div 元素。
此代码用于通过单击屏幕中的任意位置来隐藏 div 元素。
在做每件事之前,请先理解代码并复制它......
Here #suggest_input in is the name of textbox and .suggest_container is the ul class name and .suggest_div is the main div element for my auto-suggest.
this code is for hiding the div elements by clicking any where in the screen.
Before doing every thing please understand the code and copy it...
尝试这个:
Try this:
当点击发生在非子元素中时隐藏容器 div 的另一种方法;
Another way of hiding the container div when a click happens in a not children element;
试试这个,它对我来说非常有效。
Try this, it's working perfect for me.
但你也需要记住这些事情。 http://css-tricks.com/dangers-stopping-event-propagation/
but you need to keep in mind these things as well. http://css-tricks.com/dangers-stopping-event-propagation/
这是一个基于 Sandeep Pal 答案的可行 CSS/小型 JS 解决方案:
通过单击复选框然后在菜单之外进行尝试:
https://jsfiddle.net/qo90txr8/
Here is a working CSS/small JS solution based on the answer of Sandeep Pal:
Try it out by clicking the checkbox and then outside of the menu:
https://jsfiddle.net/qo90txr8/
这不起作用 - 当您单击 .myDIV 内部时,它会隐藏它。
This doesn't work - it hides the .myDIV when you click inside of it.
对上述建议只有 2 个小改进:
停止触发此事件的传播,假设它是一次点击
Just 2 small improvements to the above suggestions:
stop propagation on the event that triggered this, assuming its a click
这是由上面的其他解决方案得出的。
This is made from the other solutions above.
简单的解决方案:在某个特定元素之外的任何位置隐藏单击事件上的元素。
Simple Solution: hide an element on a click event anywhere outside of some specific element.