CSS输入字段文本输入文本的颜色

CSS输入字段文本输入文本的颜色,css,input,focus,textarea,Css,Input,Focus,Textarea,我有一个输入字段,其中文本的颜色是黑色。(我使用的是jquery.placeholder)假设其中的文本是“电子邮件” 当您单击此字段时,包含文本的黑色位置将消失(多亏了jquery.placeholder) 任何文本输入到这个字段,我希望它变成红色,当你取消选择这个字段,我希望它保持红色 目前,写着“E-Mail”的文本是黑色的,我输入的任何东西都是红色的,这很好,但一旦我取消选择,它就会变回黑色。有人能帮我吗?我希望文本保持红色,即使在取消选择后也是如此。这是我的密码 textarea:fo

我有一个输入字段,其中文本的颜色是黑色。(我使用的是jquery.placeholder)假设其中的文本是“电子邮件”

当您单击此字段时,包含文本的黑色位置将消失(多亏了jquery.placeholder)

任何文本输入到这个字段,我希望它变成红色,当你取消选择这个字段,我希望它保持红色

目前,写着“E-Mail”的文本是黑色的,我输入的任何东西都是红色的,这很好,但一旦我取消选择,它就会变回黑色。有人能帮我吗?我希望文本保持红色,即使在取消选择后也是如此。这是我的密码

textarea:focus, input:focus {
    color: #ff0000;
}

input, select, textarea{
    color: #000;
}
替换:

input, select, textarea{
    color: #000;
}
与:


color:#ff0000

将第二种样式更改为:

input, select, textarea{
    color: #ff0000;
}
此时,您正在告诉表单在焦点关闭后将文本更改为
黑色
。上述补救措施表明

此外,最好将正常状态样式放在样式表中的
:focus
:hover
样式之前。这有助于防止这个问题。所以

input, select, textarea{
    color: #ff0000;
}

textarea:focus, input:focus {
    color: #ff0000;
}

我总是做输入提示,如下所示:

    <input style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus="this.value=''; this.style.color='#000000'">
    <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>
    <input id = 0; style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus=clearField(this)>

当然,如果用户填写该字段,更改焦点并返回该字段,该字段将再次被清除。如果你那样做,一定要确保那是你想要的。 您可以通过设置信号量使其成为一次性事件,如下所示:

    <input style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus="this.value=''; this.style.color='#000000'">
    <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>
    <input id = 0; style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus=clearField(this)>

已清除[0]=已清除[1]=已清除[2]=0//为每个字段设置清除标志
函数clearField(t){//在
如果(!cleared[t.id]){//函数使其成为静态和全局的
cleared[t.id]=1;//您可以使用true和false,但这需要更多的输入
t、 值=“”;//有更多的拼写错误机会
t、 style.color='#000000';
}
}
然后,您的字段如下所示:

    <input style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus="this.value=''; this.style.color='#000000'">
    <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>
    <input id = 0; style="color: #C0C0C0;" value="someone@somewhere.com" 
    onfocus=clearField(this)>

要向输入添加颜色,请使用以下css代码:

input{
     color: black;
}

如果希望占位符文本为红色,则需要在CSS中专门针对它

写:

input::placeholder{
  color: #f00;
}

好的,我理解,但是如果我将第二个样式更改为红色,那么原始的placeholding文本也将更改为红色(而不是黑色)。这有意义吗?是的。如果你需要的文字是黑色的第一。在占位符插件中设置它。或者你可以使用一些额外的脚本来实现这一点。谢谢,伙计,我完全没有声明占位符样式,它与输入框样式是分开的!