使用 jQuery 在 DIV 外部单击时隐藏 DIV,但允许传播事件
我读过很多其他问题,涉及如何在单击外部时关闭 div,但我有一个复杂的问题,因为我有其他与 .live() 绑定的元素,
我想要发生的是如果我单击页面上的其他任何位置,div 应该消失,包括如果我单击“实时”元素。当我单击该活动元素时,我希望它的处理程序正常进行。
据我所知,处理关闭 div 的基本方法是将单击侦听器附加到 或 $document,但这会阻止任何其他实时事件冒泡通过上述 body/doc 处理程序。有办法解决这个问题吗?
I've read a lot of the other questions involving how to close a div when you click outside of it, but I have a compounded problem in that I have other elements that are bound with .live()
What I want to happen is that if I click anywhere else on the page the div should disappear, including if I click on a 'live' element. When I click on that live element I want it's handler to proceed normally.
So far as I've read, the basic way to handle closing the div is to attach a click listener to the or $document, but this would prevent any other live event from bubbling past the aforementioned body/doc handler. Is there a way around this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在现场,事件按顺序从最前面到最后面冒泡。在“实时”元素的单击(它是 body 对象的直接/间接子元素)中,如果您不返回 false 或停止传播,它将向上冒泡到达文档的单击处理程序。通过实时附加文档的点击似乎不起作用。
在此处检查示例实现:http://jsfiddle.net/VHtSP/6/
In live, events bubble up the ladder from front-most to back-most in that order. In your 'live' element's click (which is a direct/indirect child of your body object), if you don't return false or stop propagation, it will bubble up the ladder to reach the document's click handler. Attaching the document's click via live doesn't seem to work.
Check sample implementation here: http://jsfiddle.net/VHtSP/6/