jQuery 的 mouseout() 和 mouseleave() 有什么区别?

发布于 2024-10-04 17:49:50 字数 48 浏览 9 评论 0原文

jQuery 的 mouseout() 和 mouseleave() 有什么区别?

What is the difference between jQuery's mouseout() and mouseleave()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

围归者 2024-10-11 17:49:50

mouseleave 事件与 mouseout 事件的不同之处在于它处理事件冒泡的方式。如果本例中使用了 mouseout,那么当鼠标指针移出 Inner 元素时,就会触发处理程序。这通常是不受欢迎的行为。另一方面,mouseleave 事件仅在鼠标离开其绑定的元素(而不是后代元素)时触发其处理程序。因此,在此示例中,当鼠标离开 Outer 元素而不是 Inner 元素时,将触发处理程序。

来源:http://api.jquery.com/mouseleave/

The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. 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. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element.

Source: http://api.jquery.com/mouseleave/

岁吢 2024-10-11 17:49:50

有时,mouseout 可能是比 mouseleave 更好的选择。

例如,假设您创建了一个工具提示,希望将其显示在 mouseenter 上的某个元素旁边。您可以使用 setTimeout 来防止工具提示立即弹出。您可以使用 clearTimeout 清除 mouseleave 上的超时,这样如果鼠标离开,工具提示将不会显示。 这在 99% 的情况下都有效。

但是现在假设您附加了工具提示的元素是一个带有 click 事件的按钮,并且我们还假设此按钮会提示用户使用确认警报框。用户单击按钮并触发警报。用户按下它的速度足够快,以至于您的工具提示没有机会弹出(到目前为止一切顺利)。

用户按下alert框的OK按钮,然后鼠标离开该元素。但由于浏览器页面现在处于锁定状态,因此在按下“确定”按钮之前不会触发任何 JavaScript,这意味着您的 mouseleave 事件不会触发。用户按下“确定”后,将弹出工具提示(这不是您想要的)。

在这种情况下使用 mouseout 是合适的解决方案,因为它会触发。

There can be times when mouseout is a better choice than mouseleave.

For example, let's say you've created a tooltip that you want displayed next to an element on mouseenter. You use setTimeout to prevent the tooltip from popping up instantly. You clear the timeout on mouseleave using clearTimeout so if the mouse leaves the tooltip won't be displayed. This will work 99% of the time.

But now let's say the element you have a tooltip attached to is a button with a click event, and let's also assume this button prompts the user with either a confirm or alert box. The user clicks the button and the alert fires. The user pressed it fast enough that your tooltip didn't have a chance to pop up (so far so good).

The user presses the alert box OK button, and the mouse leaves the element. But since the browser page is now in a locked state, no javascript will fire until the OK button has been pressed, meaning your mouseleave event WILL NOT FIRE. After the user presses OK the tooltip will popup (which is not what you wanted).

Using mouseout in this case would be the appropriate solution because it will fire.

一个人的旅程 2024-10-11 17:49:50

jQuery API 文档:

mouseout

由于事件冒泡,这种事件类型可能会导致许多令人头痛的问题。例如,在本例中,当鼠标指针移出 Inner 元素时,将向该元素发送 mouseout 事件,然后向上滴到 Outer 元素。这可能会在不适当的时候触发绑定的 mouseout 处理程序。请参阅 .mouseleave() 的讨论以获取有用的替代方案。

所以mouseleave是一个自定义事件,就是因为上述原因而设计的。

http://api.jquery.com/mouseleave/

jQuery API doc:

mouseout

This event type can cause many headaches due to event bubbling. For instance, when the mouse pointer moves out of the Inner element in this example, a mouseout event will be sent to that, then trickle up to Outer. This can trigger the bound mouseout handler at inopportune times. See the discussion for .mouseleave() for a useful alternative.

So mouseleave is a custom event, which was designed because of the above reason.

http://api.jquery.com/mouseleave/

陪我终i 2024-10-11 17:49:50

当鼠标离开所选元素以及鼠标离开其子元素时,Mouseout 事件将触发。

当指针仅离开所选元素时,Mouseleave 元素将触发事件。

参考:W3School

Event Mouseout will trigger when mouse leaves the selected element and also when mouse leaves it's child elements also.

Event Mouseleave element will trigger when pointer will leave the selected element only.

Reference: W3School

软糯酥胸 2024-10-11 17:49:50

我使用 plan Javascript 而不是 jquery 遇到了类似的问题,但它们有一些相关性,我会留下我的两分钱,以防现在有人在搜索。

我试图在导航菜单上使用 mouseout 事件。父 div 有一个由 ul 元素列表组成的子菜单。当我尝试导航到 div 子元素时,mouseout 事件被触发。这不是我想要的输出。

来自文档

如果光标进入子元素,mouseout 也会传递到元素
元素,因为子元素遮挡了元素的可见区域
元素。

这就是问题所在。

mouseleave 事件没有这个问题。只要使用它就可以让事情对我有用。

https://developer.mozilla.org/en-US/文档/Web/API/Element/mouseleave_event

I encountered a similar problem using plan Javascript instead of jquery, but they're some how related and I'll leave my two cents in case someone else is on the search nowadays.

I was trying to use the mouseout event on a navigation menu. The parent div had a submenu composed of a list of uls elements. When I tried to navigate to the div children elements the mouseout event was fired. This was not my desired output.

From the docs

mouseout is also delivered to an element if the cursor enters a child
element, because the child element obscures the visible area of the
element.

And that was the issue.

The mouseleave event did not have this issue. Just using it made things work for me.

https://developer.mozilla.org/en-US/docs/Web/API/Element/mouseleave_event

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文