2010-03-25 106 views
5

我有一個輸入文本字段,默認情況下其值爲「something」,但是當我開始輸入時,我希望默認值更改顏色,並輸入文本,另一個。更改文本字段的文本顏色

我該怎麼做?


<input type="text" value="something" onclick="this.value=''" /> 
+0

我假設你的意思是一個輸入文本框?一個按鈕不能輸入文本。 – 2010-03-25 15:25:48

回答

13

爲了簡單起見喜歡你的例子:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" /> 

,並應幾乎做到這一點。

5

你可能想嘗試以下操作:

<input type="text" value="something" 
     onFocus="if (this.value == 'something') this.style.color = '#ccc';" 
     onKeyDown="if (this.value == 'something') { 
         this.value = ''; this.style.color = '#000'; }"> 
0

去關@拖布的答案,這是它使用jQuery和不顯眼的Javascript

 

    
$(document).ready(
    function() { 
     $("#mytext").bind(
     "click", 
     function() { 
      $(this).val(""); 
      $(this).css("color", "red"); 
     } 
    ); 
    } 
) 
 

+0

很高興! - http://jquery.com/ - http://www.learningjquery.com/ – 2010-03-25 16:23:52

-1

在這裏,我們去你會怎麼辦:

<input type="text" value="something" onclick="this.value='';this.style.color='red';" /> 

祝您好運!

保持編碼!