【问题标题】:Mouseleave inside or outside mouseenter?鼠标离开内部还是外部鼠标进入?
【发布时间】:2013-03-23 03:23:49
【问题描述】:

我正在学习 jQuery 的基础知识,当我发现 mouseleave 和 mouseenter 动作时,我开始想知道应该将 mouseleave 动作放在哪里?哪一个更正确,并且永远有效?

$(document).ready(function(){
    $('div').mouseenter(function(){
        $(this).fadeTo('fast','1');
        $(this).mouseleave(function(){
            $(this).fadeTo('fast','0.25');
        });
    });
});

或者this1更好?

$(document).ready(function(){
    $('div').mouseenter(function(){
        $(this).fadeTo('fast','1');
        });
        $('div').mouseleave(function(){
            $(this).fadeTo('fast','0.25');
        });
});

【问题讨论】:

    标签: jquery html performance


    【解决方案1】:

    您的第二个选项更正确,它应该始终设置一个事件。每次触发mouseenter 时,您的第一个选项都会添加一个新的mouseleave 事件,从而导致许多附加事件。所以使用:

    $('div').mouseenter(function () {
        $(this).fadeTo('fast', 'fast');
    });
    $('div').mouseleave(function () {
        $(this).fadeTo('fast', '0.25');
    });
    

    .hover(handlerIn, handlerOut) 实际上有一些很好的速记。

    $('div').hover(
        function () { $(this).fadeTo('fast', 'fast'); },
        function () { $(this).fadeTo('fast', '0.25'); }
    );
    

    【讨论】:

    • 当你击败我 2 秒
    • 谢谢 ;) 顺便说一句。看看第一个使用fadeTo参数'fast' + 'fast'的功能有多愚蠢:D我正在制作codecademy轨道,所以也许我稍后会了解.hover。无论如何谢谢 ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多