2012-07-17 85 views
1

我有一個文本在我的表格中提交。文本字段中文本的顏色應爲灰色。如果我點擊它並鍵入,文本應該變黑,並保持黑色。文本字段輸入顏色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=''" onBlur="if(this.value=='') this.value='Address'" value="Address"> 

現在的文字變成黑色的顏色,當我打字。但輸入後會變回灰色。打字後我需要黑色。我怎樣才能做到這一點?

+0

所以你的意思是你不想改變y的顏色我們的文字在輸入時和輸入正確後? – Kishore 2012-07-17 04:46:24

+2

爲什麼你不能在輸入字段中使用'placeholder'屬性? – 2012-07-17 04:48:59

回答

3

如何

<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"> 
+0

它的工作。非常感謝。 – designersvsoft 2012-07-17 05:15:38

+0

嗨,還有一個幫助。如果我點擊重置按鈕輸入文本應該再次變成灰色。任何建議? – designersvsoft 2012-07-17 10:00:05

0

不知道你的問題是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> 

有了這個文本是黑色和焦點它是灰色的但聚焦後它回到黑色。

0

對於以上問題,看看,請在下面寫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"> 

CSS,因爲它是:

input[type=text]{ 
    color:#999999; 
} 
input[type=text]:focus{ 
    color:#000; 
} 

,如果你想測試你的箱子然後http://codebins.com/bin/4ldqpaq