Javascript 文本字段输入颜色css

Javascript 文本字段输入颜色css,javascript,css,Javascript,Css,我的表格里有一段文字。文本字段中文本的颜色应为灰色。如果我单击它并键入,文本将变为黑色,并保持为黑色 我使用了以下css input[type=text]{ color:#999999;} input[type=text]:focus{ color:#000;} 这是我的html代码 <input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBl

我的表格里有一段文字。文本字段中文本的颜色应为灰色。如果我单击它并键入,文本将变为黑色,并保持为黑色

我使用了以下css

input[type=text]{ color:#999999;}
input[type=text]:focus{ color:#000;}
这是我的html代码

<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value=='') this.value='Address'" value="Address">

现在,当我打字时,文本变成黑色。但打字后它又变成了灰色。打字后我需要它是黑色的。我该怎么做

怎么样

<input name="address" id="address" type="text" 
       onFocus="if(this.value =='Address' )this.value='';this.style.color='#000';" 
       onBlur="if(this.value=='') this.value='Address'; if (this.value=='Address') this.style.color='#999';" 
       value="Address">

不确定您的问题是否100%清楚,但为什么不直接反转CSS中的值呢

<style>
input[type=text]{ color:#000;}
input[type=text]:focus{ color:#999999;}
</style>

<p>
<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value=='') this.value='Address'" value="Address">
</p>

输入[type=text]{color:#000;}
输入[类型=文本]:焦点{颜色:#999999;}


有了这个,文本是黑色的,在焦点上是灰色的,但在焦点之后它会返回到黑色。

我已经使用JQuery解决了您的问题。请看一下这个JSFIDLE


对于上述问题,请按以下方式编写HTML:

<input name="address" id="address" type="text" onFocus="if(this.value =='Address' ) this.value=''" onBlur="if(this.value==''){ this.value='Address'; } else{ this.style.color='#000'; }" value="Address">

如果你想测试你的bin,那么

你的意思是你不想在键入时和键入后更改文本的颜色,对吗?为什么不能在输入字段中使用
占位符
属性?您好,还有一个帮助。如果单击重置按钮,输入文本应再次变为灰色。有什么建议吗?
input[type=text]{
  color:#999999;
}
input[type=text]:focus{
  color:#000;
}