如何在 jQuery 中触发两个元素的 mouseout 事件?
假设我有两个矛状 div,A 和 B,它们在一个角重叠:
+-----+
| |
| A |
| +-----+
+---| |
| B |
| |
+-----+
我想在鼠标离开 A 和 B 时触发一个事件。
我尝试了这个
$("#a, #b").mouseleave(function() { ... });
,但是如果鼠标离开任一节点。我希望一旦鼠标不在任何一个节点上就触发该事件。
有没有简单的方法可以做到这一点?我有一个想法,涉及全局变量来跟踪每个 div 上的鼠标状态,但我希望有更优雅的东西。
Let's suppose I have two spearate divs, A and B, which overlap at a corner:
+-----+
| |
| A |
| +-----+
+---| |
| B |
| |
+-----+
I want to trigger an event when the mouse leaves both A and B.
I tried this
$("#a, #b").mouseleave(function() { ... });
But this triggers the event if the mouse leaves either node. I want the event to be triggered once the mouse is not over either node.
Is there an easy way to do this? I had an idea that involved global variables keeping track of the mouse state over each div, but I was hoping for something more elegant.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我一直遇到这个问题,如果它适合我正在做的事情,我的“快速修复”如下;
小提琴:http://jsfiddle.net/adeneo/LdDBn/
I encounter this problem all the time, and my 'quick fix' if it fits what I'm doing is the following;
Fiddle : http://jsfiddle.net/adeneo/LdDBn/
如果将第二个容器嵌套在第一个容器中,则不需要复杂的 jQuery 解决方案:
http://jsfiddle.net /5cKSf/3/
HTML
CSS
JS
If you nest the second container inside the first there is no need for a complex jQuery solution:
http://jsfiddle.net/5cKSf/3/
HTML
CSS
JS
我想你可以使用类似的方法来实现这一点:
I guess you can achieve this using something like: