简体   繁体   English

Javascript Mouseover从孩子们冒泡

[英]Javascript Mouseover bubbling from children

Ive got the following html setup: 我有以下html设置:

<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. 它的风格,所以你不能悬停div1而不悬停其他2个div之一。 Now i've got a mouseout on div1. 现在我在div1上有一个mouseout。
The problem is that my div1.mouseout gets triggered when i move from content1 to content2, because their mouseouts are bubbling. 问题是当我从content1移动到content2时,我的div1.mouseout被触发,因为它们的mouseout正在冒泡。
and the event's target, currentTarget or relatedTarget properties are never div1, since it is never hovered directly... 并且事件的target,currentTarget或relatedTarget属性永远不会是div1,因为它永远不会直接悬停...
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. 当鼠标离开div1时,只能触发div1的mouseout。

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... 将mouseout更改为mouseleave并且代码工作正常...

Use the mouseleave event instead or mouseout for this, it handles your specific issue. 请改用mouseleave事件或mouseout ,它会处理您的特定问题。 See here for details 详情请见此处

From the docs on the difference: 从关于差异的文档:

The mouseleave event differs from mouseout in the way it handles event bubbling. mouseleave事件与mouseout处理事件冒泡的方式不同。 If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. 如果在此示例中使用了mouseout,那么当鼠标指针移出Inner元素时,将触发处理程序。 This is usually undesirable behavior. 这通常是不受欢迎的行为。 The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. 另一方面,mouseleave事件仅在鼠标离开它所绑定的元素时触发其处理程序,而不是后代。 So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element. 因此,在此示例中,处理程序在鼠标离开Outer元素时触发,而不是Inner元素。

Example markup: 示例标记:

<div id="outer">
  Outer
  <div id="inner">
    Inner
  </div>
</div>

The hover method has two parameters, first for mouse in and second for mouse out. hover方法有两个参数,第一个用于鼠标输入,第二个用于鼠标输出。

$('your_div').hover(function(){
  // your code here.
}, function(){// any mouse out code here})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM