jQuery 的 mouseout() 和 mouseleave() 有什么区别?
jQuery 的 mouseout() 和 mouseleave() 有什么区别?
What is the difference between jQuery's mouseout() and mouseleave()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
来源:http://api.jquery.com/mouseleave/
Source: http://api.jquery.com/mouseleave/
有时,
mouseout
可能是比mouseleave
更好的选择。例如,假设您创建了一个工具提示,希望将其显示在
mouseenter
上的某个元素旁边。您可以使用setTimeout
来防止工具提示立即弹出。您可以使用clearTimeout
清除mouseleave
上的超时,这样如果鼠标离开,工具提示将不会显示。 这在 99% 的情况下都有效。但是现在假设您附加了工具提示的元素是一个带有
click
事件的按钮,并且我们还假设此按钮会提示用户使用确认
或警报
框。用户单击按钮并触发警报
。用户按下它的速度足够快,以至于您的工具提示没有机会弹出(到目前为止一切顺利)。用户按下
alert
框的OK按钮,然后鼠标离开该元素。但由于浏览器页面现在处于锁定状态,因此在按下“确定”按钮之前不会触发任何 JavaScript,这意味着您的mouseleave
事件不会触发。用户按下“确定”后,将弹出工具提示(这不是您想要的)。在这种情况下使用 mouseout 是合适的解决方案,因为它会触发。
There can be times when
mouseout
is a better choice thanmouseleave
.For example, let's say you've created a tooltip that you want displayed next to an element on
mouseenter
. You usesetTimeout
to prevent the tooltip from popping up instantly. You clear the timeout onmouseleave
usingclearTimeout
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 aconfirm
oralert
box. The user clicks the button and thealert
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 yourmouseleave
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.jQuery API 文档:
mouseout
所以
mouseleave
是一个自定义事件,就是因为上述原因而设计的。http://api.jquery.com/mouseleave/
jQuery API doc:
mouseout
So
mouseleave
is a custom event, which was designed because of the above reason.http://api.jquery.com/mouseleave/
当鼠标离开所选元素以及鼠标离开其子元素时,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
我使用 plan Javascript 而不是 jquery 遇到了类似的问题,但它们有一些相关性,我会留下我的两分钱,以防现在有人在搜索。
我试图在导航菜单上使用
mouseout
事件。父div
有一个由ul
元素列表组成的子菜单。当我尝试导航到div
子元素时,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 parentdiv
had a submenu composed of a list oful
s elements. When I tried to navigate to thediv
children elements themouseout
event was fired. This was not my desired output.From the docs
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