输入文本的 CSS 输入字段文本颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6669846/
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
CSS Input field text color of inputted text
提问by John
I have an input field, and the color of the text in it is black. (I'm using jquery.placeholder) Let's say the text in there is "E-Mail"
我有一个输入字段,其中文本的颜色是黑色。(我正在使用 jquery.placeholder)让我们说那里的文字是“电子邮件”
When you click on this field, the black placeholding text dissapears (thanks to jquery.placeholder).
当您单击此字段时,黑色占位文本会消失(感谢 jquery.placeholder)。
Any text inputted into this field, I want it to turn red, and when you deselect this field, I want it to stay red.
输入到此字段的任何文本,我希望它变成红色,当您取消选择此字段时,我希望它保持红色。
At the moment, the text saying "E-Mail" is black, and anything I type into is red, which is great, but as soon as i deselect, it goes back to black. Can anyone help me? I want the text to stay red, even after it's deselected. Here is my code
目前,说“电子邮件”的文本是黑色的,我输入的任何内容都是红色的,这很好,但是一旦我取消选择,它就会变回黑色。谁能帮我?我希望文本保持红色,即使在取消选择之后也是如此。这是我的代码
textarea:focus, input:focus {
color: #ff0000;
}
input, select, textarea{
color: #000;
}
回答by Jason Gennaro
Change your second style to this:
将您的第二个样式更改为:
input, select, textarea{
color: #ff0000;
}
At the moment, you are telling the form to change the text to black
once the focus is off. The above remedies that.
目前,您正在告诉表单black
在焦点关闭后将文本更改为。以上补救措施。
Also, it is a good idea to place the normal state styles ahead of the :focus
and :hover
styles in your stylesheet. That helps prevent this problem. So
此外,将正常状态样式放在样式表中的:focus
和:hover
样式之前也是一个好主意。这有助于防止这个问题。所以
input, select, textarea{
color: #ff0000;
}
textarea:focus, input:focus {
color: #ff0000;
}
回答by Dino Senjkovic
If you want the placeholder text to be red you need to target it specifically in CSS.
如果您希望占位符文本为红色,则需要在 CSS 中专门针对它。
Write:
写:
input::placeholder{
color: #f00;
}
回答by oldmoses
I always do input prompts, like this:
我总是做输入提示,像这样:
<input style="color: #C0C0C0;" value="[email protected]"
onfocus="this.value=''; this.style.color='#000000'">
Of course, if your user fills in the field, changes focus and comes back to the field, the field will once again be cleared. If you do it like that, be sure that's what you want. You can make it a one time thing by setting a semaphore, like this:
当然,如果您的用户填写该字段,更改焦点并返回该字段,该字段将再次被清除。如果您这样做,请确保这就是您想要的。您可以通过设置信号量使其成为一次性的事情,如下所示:
<script language = "text/Javascript">
cleared[0] = cleared[1] = cleared[2] = 0; //set a cleared flag for each field
function clearField(t){ //declaring the array outside of the
if(! cleared[t.id]){ // function makes it static and global
cleared[t.id] = 1; // you could use true and false, but that's more typing
t.value=''; // with more chance of typos
t.style.color='#000000';
}
}
</script>
Your <input> field then looks like this:
您的 <input> 字段如下所示:
<input id = 0; style="color: #C0C0C0;" value="[email protected]"
onfocus=clearField(this)>
回答by Codemaker
To add color to an input, Use the following css code:
要为输入添加颜色,请使用以下 css 代码:
input{
color: black;
}
回答by angrydust
replace:
代替:
input, select, textarea{
color: #000;
}
with:
和:
input, select, textarea{
color: #f00;
}
or color: #ff0000;
或者 color: #ff0000;