Javascript 鼠标悬停从孩子冒泡
我有以下 html 设置:
<div id="div1">
<div id="content1">blaat</div>
<div id="content1">blaat2</div>
</div>
它的样式设置为您不能在不悬停其他 2 个 div 之一的情况下悬停 div1。 现在我在 div1 上有了鼠标悬停。
问题是当我从 content1 移动到 content2 时,我的 div1.mouseout 被触发,因为它们的 mouseout 正在冒泡。
并且事件的目标、currentTarget 或 relatedTarget 属性永远不会是 div1,因为它永远不会直接悬停...
我一直在疯狂地寻找这个,但我只能找到与我需要的相反的问题的文章和解决方案。这看起来微不足道,但我无法让它工作......
div1 的 mouseout 仅应在鼠标离开 div1 时触发。
一种可能性是在鼠标输入和鼠标离开上设置一些数据,但我相信这应该开箱即用,因为它只是一个鼠标离开...
编辑:
bar.mouseleave(function(e) {
if ($(e.currentTarget).attr('id') == bar.attr('id')) {
bar.css('top', '-'+contentOuterHeight+'px');
$('#floatable-bar #floatable-bar-tabs span').removeClass('active');
}
});
将鼠标输出更改为鼠标离开并且代码有效。 ..
Ive got the following html setup:
<div id="div1">
<div id="content1">blaat</div>
<div id="content1">blaat2</div>
</div>
it is styled so you can NOT hover div1 without hovering one of the other 2 divs.
Now i've got a mouseout on div1.
The problem is that my div1.mouseout gets triggered when i move from content1 to content2, because their mouseouts are bubbling.
and the event's target, currentTarget or relatedTarget properties are never div1, since it is never hovered directly...
I've been searching mad for this, but I can only find articles and solutions for problems who are the reverse of what I need. It seems trivial but I can't get it to work...
The mouseout of div1 should ONLY get triggered when the mouse leaves div1.
One of the possibilities would be to set some data on mouse enter and mouseleave, but I'm convinced this should work out of the box, since it is just a mouseout...
EDIT:
bar.mouseleave(function(e) {
if ($(e.currentTarget).attr('id') == bar.attr('id')) {
bar.css('top', '-'+contentOuterHeight+'px');
$('#floatable-bar #floatable-bar-tabs span').removeClass('active');
}
});
changed the mouseout to mouseleave and the code worked...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,请使用
mouseleave
事件或mouseout
来处理您的特定问题。 请参阅此处了解详细信息来自有关差异的文档:
标记示例:
Use the
mouseleave
event instead ormouseout
for this, it handles your specific issue. See here for detailsFrom the docs on the difference:
Example markup:
hover
方法有两个参数,第一个参数用于鼠标进入,第二个参数用于鼠标移出。The
hover
method has two parameters, first for mouse in and second for mouse out.