2014-11-05 103 views
0

我想在更改另一个元素时更改CSS display属性。 我试着用jQuery像来实现:更改悬停时的显示属性

$("li.megamenu-li-first-level").hover(function() { 
    $(".mega").css("display", "show"); 

}); 

但我的元素是在html<div class="mega" style="display: none;">仍然没有露面定义,为什么不呢?

回答

1

没有有效的value可用与display财产调用show。您可能希望写这$(".mega").css("display", "block");但可以简单地写为$(".mega").show()

尝试,

$("li.megamenu-li-first-level").hover(function() { 
    $(".mega").show(); 
}); 

显示和隐藏鼠标悬停的元素。

$("li.megamenu-li-first-level").hover(function() { 
     $(".mega").show(); 
    },function(){ 
     $(".mega").hide(); 
    }); 
+0

你也可以告诉我,当光标离开元素时,我怎么再次隐藏'.mega'? – cerr 2014-11-05 05:28:55

+0

@cerr查看更新的答案。 – 2014-11-05 05:31:34

1

可以试试这个:

$("li.megamenu-li-first-level").hover(function() { 
    $(".mega").css("display", "block"); 

}); 

希望它能帮助。

1

试试这个,在鼠标悬停和鼠标离开将是有用的。

$('.li.megamenu-li-first-level').mouseover(function(){ 
if ($('.mega').css('display') == 'none') { 
    $('.mega').css("display","block"); 
} 

});

$('.li.megamenu-li-first-level').mouseleave(function(){ if ($('.mega').css('display') == 'block') { $('.mega').css("display","none");enter code here } });

0

这是最终的工作最适合我:

$("li.megamenu-li-first-level").mouseover(function() { 
    $(".mega").show(); 
}); 
$("li.megamenu-li-first-level").mouseleave(function() { 
    $(".mega").hide(); 
}); 

谢谢大家的帮助,我能够使用的点点滴滴,从大家!