3

我只在 Firefox 中遇到了 mosueenter/mouseleave 事件的问题...

http://www.screencast.com/users/StanleyGoldman/folders/Jing/media/be3572de-9c72-4e2a-8ead-6d29b0764709

<HTML>
    <HEAD>
        <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.js"></script>
        <script>
            $(document).ready(function() {
                $("#theDiv").mouseenter(function() {
                    $("#output").append('mouseenter<br>'); 
                });
                $("#theDiv").mouseleave(function() {
                    $("#output").append('mouseleave<br>');
                });
            });

        </script>
    </HEAD>
    <BODY>

    <div id="theDiv" style="position:relative; height: 300px; width:300px; background-color:Black;">
        <input type="text" style="position:absolute; top: 40px;" />

        <div style="position:absolute; top:100px; height:100px; width:100px; background-color:Red; overflow:auto;">
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        <p>Content</p>
        </div>
    </div>

    <div id="output"></div>

    </BODY>
</HTML>

我只想知道鼠标何时离开包含的 DIV。但是,如果您将鼠标快速移动到文本框上或将鼠标移动到 div 的滚动条上,事件就会触发。

- 编辑 -

$("#theDiv").hover(function() {
    $("#output").append('hoverin<br>');
}, function() {
    $("#output").append('hoverout<br>');
});

我用悬停尝试了以下操作。它似乎只在 Firefox 中遇到同样的问题。

4

2 回答 2

2

此行为与Firefox 3.6 中已修复的Firefox 错误有关。jQuery 尝试使用 insideElement 函数(通过 jQuery 源代码搜索)来处理这个错误,但解决方案并不完美。

于 2010-01-28T22:39:53.850 回答
2

如果您打算在鼠标进入和离开元素时同时执行操作,我几乎总是发现使用hover()方法或hoverIntent()插件而不是单独的 mouseenter/mouseleave 处理程序更好。这两个似乎都处理可能的鼠标移动事件范围,而不仅仅是映射到 mouseenter/mouseleave。

于 2010-01-28T22:04:39.230 回答