如何使用 jQuery 更改文本框的背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1223711/
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
How to use jQuery to change the background color of a textbox?
提问by Dykam
How to use jQuery to change the backgound color of a textbox?
如何使用 jQuery 更改文本框的背景颜色?
回答by Dykam
Better practice is to seperate UI from logic, in your case:
更好的做法是将 UI 与逻辑分开,在您的情况下:
$("#textboxid").addClass("aClass");
If you really need it your way, then do the following:
如果您真的需要它,请执行以下操作:
$("#textboxid").css({"background-color": "color"});
Replace #textboxid
with the desired selector, and color with the desired color.
替换#textboxid
为所需的选择器,并使用所需的颜色进行着色。
Note the following does the same for one property:
请注意,以下对一个属性执行相同的操作:
$("#textboxid").css("background-color", "color");
回答by Joe Davis
$('#txtBoxID').css('background-color', '#ffff00');
回答by redsquare
Better to add a class name to the input rather than hard coding styles into your js. Presentation styles should reside in css not js.
最好在输入中添加类名,而不是将样式硬编码到 js 中。演示文稿样式应该驻留在 css 而不是 js 中。
$('#inputId').addClass('someCssClass');
回答by Andrew
Better yet, don't use jQuery. I assume you want to do this on focus or on blur, so you can do:
更好的是,不要使用 jQuery。我假设您想在焦点或模糊上执行此操作,因此您可以执行以下操作:
#textboxid {background-color:defaultcolor;}
#textboxid:focus {background-color:selectedcolor;}