2010-03-25 36 views
5

Tengo un campo de entrada de texto, que tiene un valor "algo" por defecto, pero cuando empiezo a escribir, quiero que el valor predeterminado cambie de color, y el texto que escribo, otro.Cambiar el color del texto en el campo de texto

¿Cómo puedo hacer eso?


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

Supongo que se refiere a un cuadro de texto de entrada? Un botón no puede tener texto escrito en él. –

Respuesta

13

que sea sencillo como tu ejemplo:

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

y que debe más o menos hacerlo.

5

Es posible que desee probar el siguiente:

<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

Dejar de tomar la respuesta de @ Chibu, esta es la forma en que lo haría con jQuery y discreto Javascript

 

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

+0

con mucho gusto! - http://jquery.com/ - http://www.learningjquery.com/ –

-1

Aquí vamos:

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

mejor de las suertes!

Mantenga la codificación!

Cuestiones relacionadas