从以下位置获取css值:使用.css() – jqueryhover

所以我设置了一个具有不同背景的菜单:hover颜色。 按钮背景将为灰色,并将按钮animate()hover在其各自的颜色上。

我可以抓住我hover在其上的任何按钮的原始默认颜色,如下所示:

var origBackgroundColor = $(this).css('background-color'); 

但是有可能从我设置的css:hover属性中获取颜色吗? 我将为每个按钮设置:hover颜色,因为如果有人没有启用JS,导航仍然会有:hover效果有效。

像这样的东西(或者是另一种方式):

 var hoverColor = $(this).css('background-color:hover'); 

有任何想法吗? 这可能吗?

我很确定为了获得:hover伪的background-color ,它首先需要一个浏览器事件来应用样式。 换句话说,我认为你用鼠标hover你可以得到它,但直到那时才能。

可能是你可以等到用户进行hover,然后获取颜色,用默认值覆盖它,存储它以供将来参考,然后做动画,但这可能比简单地协调你的JS和CSS更麻烦。

像这样: http : //jsfiddle.net/UXzx2/

  // grab the default color, and create a variable to store the hovered. var originalColor = $('div').css('background-color'); var hoverColor; $('div').hover(function() { // On hover, if hoverColor hasn't yet been set, grab the color // and override the pseudo color with the originalColor if( !hoverColor ) { hoverColor = $(this).css('background-color'); $(this).css('background-color',originalColor); } // Then do your animation $(this).animate({backgroundColor:hoverColor}); }); 
  // How about writing anonymous function using JQuery, // that encapsulate the originalColor: $(function() { var originalColor = $('div').css('background-color'); $('div').hover( function() { $(this).css('background-color','cyan'); }, function () { $(this).css('background-color', originalColor); } ); }); 

jQuery几乎与每个css选择器一起使用,所以在这种情况下,这应该可以正常工作。 请尝试以下代码。

 $("li").hover(function(){ var color = $(this).find(".link").css("color"); console.log(color); }); 
 a{ color: white; background-color: rgb(0, 0, 0); font-weight: bold; padding: 10px 20px; } a:hover{ color: rgb(255, 165, 0); }