jQuery 如何在jquery中设置背景颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4780670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:55:28  来源:igfitidea点击:

How to set background color in jquery

jquerycss

提问by Ulhas Tuscano

How to set background color of tdin jQuery?

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

e.g $(this).css({**BackgroundColor:Red**})

例如 $(this).css({**BackgroundColor:Red**})

Thanks

谢谢

回答by reko_t

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

回答by jAndy

You actually got it. Just forgot some quotes.

你真的明白了。只是忘记了一些报价。

$(this).css({backgroundColor: 'red'});

or

或者

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

You don't need to pass over a map/object to set only one property. You can just put pass it as string. Note that if passing an object you cannot use a -. All CSS properties which have such a character are mapped with capital letters.

您不需要传递地图/对象来仅设置一个属性。你可以把它作为字符串传递。请注意,如果传递对象,则不能使用-. 所有具有此类字符的 CSS 属性都用大写字母映射。

Reference: .css()

参考:.css()

回答by BvdVen

How about this:

这个怎么样:

$(this).css('background-color', '#FFFFFF');

Related post: Add background color and border to table row on hover using jquery

相关文章:使用 jquery 在悬停时为表格行添加背景颜色和边框

回答by Daniel Hernan Vargas Osorio

Try this for multiple CSS styles:

对多种 CSS 样式试试这个:

$(this).css({
    "background-color": 'red',
    "color" : "white"
});

回答by Waseem Khalid

You can add your attribute on callback function ({key} , speed.callback, like is

您可以在回调函数上添加您的属性({key},speed.callback,就像是

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