最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Problem with jQuery MouseenterMouseleave in Firefox - Stack Overflow

programmeradmin0浏览0评论

I'm having a problem with the mosueenter/mouseleave events only in Firefox...

<HTML>
    <HEAD>
        <script type="text/javascript" src=".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>

I only want to know when the mouse has left the containing DIV. But if you either move the mouse over a textbox really fast or move the mouse over the scrollbar of a div, the events fire.

--EDIT--

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

I tried the following with hover. It seems to suffer from the same issue only in Firefox.

I'm having a problem with the mosueenter/mouseleave events only in Firefox...

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

<HTML>
    <HEAD>
        <script type="text/javascript" src="http://code.jquery./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>

I only want to know when the mouse has left the containing DIV. But if you either move the mouse over a textbox really fast or move the mouse over the scrollbar of a div, the events fire.

--EDIT--

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

I tried the following with hover. It seems to suffer from the same issue only in Firefox.

Share Improve this question edited Jan 28, 2010 at 22:11 Stanley.Goldman asked Jan 28, 2010 at 21:58 Stanley.GoldmanStanley.Goldman 4,3372 gold badges24 silver badges25 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

This behavior is related to a firefox bug, that has been fixed in Firefox 3.6. jQuery tries to handle this bug with withinElement function (search through jQuery source code), but the solution isn't perfect.

I almost always find that it's better to use the hover() method or the hoverIntent() plugin rather than separate mouseenter/mouseleave handlers if you are planning to do things both when the mouse enters and leaves an element. Both of these seem to handle the range of possible mouse movement events rather than just mapping onto mouseenter/mouseleave.

发布评论

评论列表(0)

  1. 暂无评论