使用javascript更改文本框的文本颜色

Change textbox text color with javascript

本文关键字:文本 颜色 javascript 使用      更新时间:2023-09-26

我有一个文本框。当我点击这个时,我想用javascript改变文本颜色样式。在此之前,我成功地制作了这个。当有人点击文本框时,文本框内部清除,文本框变成旧版本模糊。这段代码现在为我工作了

<input id="kullanici_adi_text" type="text" name="kullanici_adi_text"    value="Kullanıcı İsmini Giriniz..." onfocus="if(this.value=='Kullanıcı İsmini Giriniz...') {this.value='';}  onblur="if(this.value==''){this.value='Kullanıcı İsmini Giriniz...'}"/> 

但我想改变文本的颜色也和文本框的边界大小,当有人关注这个。代码不工作

<input id="kullanici_adi_text" type="text" name="kullanici_adi_text" value="Kullanıcı İsmini Giriniz..." onfocus="if(this.value=='Kullanıcı İsmini Giriniz...') {this.value=''; document.getElementById('kullanici_adi_text').style.color = 'blue';}"  onblur="if(this.value==''){this.value='Kullanıcı İsmini Giriniz...'; document.getElementById('kullanici_adi_text').style.color = #fff; }"/> 

您应该使用颜色配额:

<input id="kullanici_adi_text" type="text"
   name="kullanici_adi_text"
   value="Kullanıcı İsmini Giriniz..." 
onfocus="if(this.value=='Kullanıcı İsmini Giriniz...')
    {this.value='';
     this.style.color = 'blue';}"  
onblur="if(this.value=='')
    {this.value='Kullanıcı İsmini Giriniz...';
     this.style.color = '#ff0000';}"/>

作为一个建议,最好将javascript代码与标记分开。

可以
<input id="kullanici_adi_text" type="text" onfocus="myFunction()">

<script>
function myFunction() {
document.getElementById("kullanici_adi_text").style.color = "#ff0000";
document.getElementById("kullanici_adi_text").style.color = "magenta";
document.getElementById("kullanici_adi_text").style.color = "blue";
document.getElementById("kullanici_adi_text").style.color = "lightblue";
}
</script>

1。在你的文档中包含jQuery库

2。包括这个脚本:

$(document).ready(function(){
   $('#kullanici_adi_text').focus(function(){
       $(this).css('color', 'red');
   }).focusout(function(){
       $(this).css('color', 'black');
   });
});