我的代码有一个小问题。我有一堆具有不同背景颜色的类元素,名为“jq”。我在悬停时为背景颜色设置动画,然后将其恢复为原始颜色:这是 .css('backgroundColor') 应该获得悬停的任何给定 div 和 jq 类的颜色的地方。 相反,我将 div 背景恢复为白色。这是我的代码:
$(document).ready(function(){
$(".jq").hover(
var bgcol = $(this).css('backgroundColor');
function(){
$(this).animate({
backgroundColor: "#EAEAEA",
color:"#333"
},trans);
},
function() {
$(this).animate({
backgroundColor:'bgcol',
color:"#888"
},trans);
});
});
最佳答案
这里有语法错误
$(document).ready(function(){
$(".jq").hover(
function(){
var bgcol = $(this).css('backgroundColor');
$(this).animate({
backgroundColor: "#EAEAEA",
color:"#333"
}, trans).data('hoverbackground', bgcol);
},
function() {
$(this).animate({
backgroundColor: $(this).data('hoverbackground'),
//backgroundColor: "#EFEFEF",
color:"#888"
}, trans).removeData('hoverbackground');
});
});
演示:Fiddle
关于jQuery .css ('backgroundColor' ) 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18271271/