js动态设置输入框字体/颜色

来源:互联网 发布:白夜追凶网络剧 编辑:程序博客网 时间:2024/05/16 05:34
[html] view plaincopy
  1. 动态设置文本框颜色:  
[html] view plaincopy
  1. 主要是利用javascript中的触发事件onfocus和onblur  
  2.   
  3. <script language="javascript" type="text/javascript">  
  4.       <!--  
  5.          function myFocus(obj,color){  
  6.   
  7.              //判断文本框中的内容是否是默认内容  
  8.   
  9.   
  10.              if(obj.value=="请输入收件人地址"){  
  11.                obj.value="";  
  12.              }  
  13.   
  14.              //设置文本框获取焦点时候背景颜色变换  
  15.              obj.style.backgroundColor=color;  
  16.          }  
  17.   
  18.   
  19.          function myblur(obj,color){  
  20.   
  21.              //当鼠标离开时候改变文本框背景颜色  
  22.              obj.style.background=color;  
  23.          }  
  24.   
  25.    
  26.   
  27. 在input标签中  
  28.   
  29. <input type="text" name="username" id="username" onfocus="myFocus(this,'#f4eaf1')" onblur="myblur(this,'white')" value="请输入收件人地址"/>  
  30.   
  31. 用上述简单方法可以做到文本框背景颜色的变换和提示信息的清除  

转自:http://blog.sina.com.cn/s/blog_78c47a0d0100qiia.html

 

[html] view plaincopy
  1. 动态设置字体颜色  
[html] view plaincopy
  1. <html>  
  2.   
  3. <body>  
  4. <script language="javascript" type="text/javascript">  
  5. function test(obj)  
  6. {  
  7.     if( obj.value!="test" ){  
  8.         document.getElementById("inputbox").className"input_s1";  
  9.     }else{  
  10.         document.getElementById("inputbox").className = "input_s2";  
  11.     }  
  12. }  
  13. </script>  
  14. <style>     
  15.   .input_s1 {font-size:20;color:red; background-color:;border-top-width:1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}    
  16.   .input_s2 {font-size:20;color:black; background-color:;border-top-width:1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px}    
  17.  </style>  
  18. <center>  
  19.   
  20. <br>  
  21. <form method="get" action="returnpage.php" >  
  22. <input id="inputbox" type='text' class="input_s1"  value="test"  maxlength='300' size='40'  name='qw' onclick="test(this)"/>  
  23. <input type="submit" value="搜一下">  
  24. <br>  
  25. </center>  
  26. </body>  
  27. </html>  
  28.   
  29. 转自:<a href="http://topic.csdn.net/u/20080804/19/dec089b3-59e5-481c-b5d8-b4e3c4949078.html">http://topic.csdn.net/u/20080804/19/dec089b3-59e5-481c-b5d8-b4e3c4949078.html</a>  

 

自己修改的一个,功能:文本框默认字体浅色,获取焦点时候清空文本框,输入文字变黑色,失去焦点判断文本框,重新回到浅色字体

function test(obj)
{
    if( obj.value!="CAS/NAME/CATALOG" ){
     document.getElementById("productParam").value="";
        document.getElementById("productParam").className="input_s2";
    }else{
     document.getElementById("productParam").value="";
        document.getElementById("productParam").className ="input_s2";
    }
}

function onBluet(obj){
 if(obj.value == ""){
  document.getElementById("productParam").value="CAS/NAME/CATALOG";
  document.getElementById("productParam").className ="input_s1";
 }else{
  document.getElementById("productParam").className ="input_s1";
 }
}

 

<style>   
  .input_s1 {font-size:20;color:#949494; background-color:;border-top-width:1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px} 
  .input_s2 {font-size:20;color:black; background-color:;border-top-width:1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px} 
 </style>

 

<input class="input_s1" id="productParam" name="productParam" onkeyup="enterLogin(event);" type="text" value="CAS/NAME/CATALOG" onclick="test(this)" onblur="onBluet(this)"/></td>


转载自:http://blog.csdn.net/yy_2011/article/details/7663342



0 0