如何在jquery中设置背景颜色

148

如何在jQuery中设置td的背景颜色?

例如:$(this).css({**backgroundColor: 'red'**})

谢谢


3
以下是对下面各种答案的补充说明:http://api.jquery.com/css/ - T.J. Crowder
5个回答

318
$(this).css('background-color', 'red');

69

你已经理解了。只是忘记了一些引号。

$(this).css({backgroundColor: 'red'});
或者
$(this).css('background-color', 'red');

您不需要传递一个映射/对象来设置一个属性。您只需将其作为字符串传递即可。请注意,如果传递一个对象,则不能使用-。所有具有这种字符的CSS属性都会映射为大写字母。

参考:.css()


1
谢谢您!我在尝试使用 $(this).css({background-color: color}) 进行操作,不知道为什么它没有生效。非常感谢! - FunkyMonk91

16

让我 ^^.

- Andrew

6
尝试多种CSS样式的方法如下所示:
$(this).css({
    "background-color": 'red',
    "color" : "white"
});

你可以在回调函数中添加animate属性,或者使用animate.css库来获取更多的动画样式。 - Daniel Hernan Vargas Osorio

0

您可以在回调函数({key} , speed.callback)上添加您的属性,例如:

$('.usercontent').animate( {
    backgroundColor:'#ddd',
},1000,function () {
    $(this).css("backgroundColor","red")
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接