Knockout.js 事件绑定 - mouseover 和 mouseout 事件的意外行为

发布于 2025-01-08 17:20:20 字数 546 浏览 4 评论 0原文

我看到的问题是,当我执行以下操作时,鼠标悬停事件似乎不会冒泡:

<div data-bind='events: {mouseover: someFunc, mouseout: someOtherFunc}'>
    <div data-bind='text: someText'></div>
</div>

我看到的是,当我最初悬停时, someFunc 函数会触发(我正在使用这些函数将类应用于获得悬停效果)。但是,当我的光标进入内部 div 时,即使 mouseover 事件应该从内部 div 冒泡到外部 div,我的 mouseout 函数也会触发。

这个小提琴演示了这个问题: http://jsfiddle.net/cSBcC/1/

在小提琴中,只需尝试将鼠标悬停在各个li 的,当光标进入内部 div 时,“hover”类被删除,即使我们仍然将鼠标悬停在 li 上。

有什么想法吗?

The issue I'm seeing is that mouse over events don't seem to bubble when I do something like:

<div data-bind='events: {mouseover: someFunc, mouseout: someOtherFunc}'>
    <div data-bind='text: someText'></div>
</div>

What I'm seeing is when I initially hover, the someFunc function fires (i'm using these functions to apply a class to get a hover effect). However, when my cursor enters the inner div, my mouseout function fires, even though the mouseover event should bubble from the inner div to the outer div.

This fiddle demonstrates the issue:
http://jsfiddle.net/cSBcC/1/

In the fiddle, just try mousing over the various li's and when the cursor gets into the inner div the "hover" class is removed, even though we are still mouseover on the li.

Any ideas?

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

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

发布评论

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

评论(1

素食主义者 2025-01-15 17:20:20

如果您引用 jQuery(确实如此),那么您可以使用 mouseleave 而不是 mouseout,因为它的行为就像您所期望的那样。

即使从外部元素移动到内部元素,mouseout 事件也会触发。

这是使用 mouseleave 的示例: http://jsfiddle.net/rniemeyer/KUNcf/

另一种选择是仅使用 CSS 并删除事件绑定,例如: http://jsfiddle.net/rniemeyer/KUNcf/2/

li.name:hover {
    background-color: yellow;        
}

If you are referencing jQuery (which you are), then you can use mouseleave instead of mouseout as it will behave like you are expecting.

mouseout events fire even when moving from an outer element to an inner element.

Here is your sample using mouseleave: http://jsfiddle.net/rniemeyer/KUNcf/

The other option is to just use CSS and remove the event binding like: http://jsfiddle.net/rniemeyer/KUNcf/2/

li.name:hover {
    background-color: yellow;        
}

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