【问题标题】:Simple Jquery Hover Issue简单的 Jquery 悬停问题
【发布时间】:2011-01-28 06:45:20
【问题描述】:

我有一些非常简单的 jquery,如下所示:

$(".block").hover(function(){
    $(".drag").stop().fadeIn(1000);                
}, function(){
    $(".drag").stop().fadeOut(1000);
});

我有一系列 div,每个都有一个 block 类。当我将鼠标悬停在.block 上时,.drag 会在所有 div 中淡入一个块类。我只想让.drag 淡入鼠标悬停在的元素上,而不是全部。谢谢!

【问题讨论】:

    标签: jquery css hover


    【解决方案1】:

    假设您想要的.drag 元素是悬停元素的后代:

    $(".block").hover(function(){
        $(this).find(".drag").stop().fadeIn(1000);                
    }, function(){
        $(this).find(".drag").stop().fadeOut(1000);
    });
    

    【讨论】:

    • 应该设置为 stop(true,true) 以避免一些错误的行为
    【解决方案2】:

    使用委托怎么样?

    你不能使用悬停,你需要 mousein 和 mouseout JQuery API -> Delegate()。 API 文档特别提到了悬停事件。

     // something more specific than body would probably be good, but some 
     // common parent of these divs
     $('body').delegate('.block', 'mousein', function() {
         $(this).find(".drag").stop().fadeIn(1000); 
     });
    
     $('body').delegate('.block', 'mouseout', function() {
         $(this).find(".drag").stop().fadeOut(1000);
     });
    

    【讨论】:

    • 好主意,但上面的答案正是我想要的。如果我需要使用 .live 的好处,我会使用它
    • 我猜你是对的。只是想我会把它扔进去。很高兴你得到你的答案@JCHASE11!
    猜你喜欢
    • 2010-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多