jQuery實現input文本框內灰色提示文本效果 和 input標籤獲取焦點是文本框內提示信息清空

一、jQuery實現input文本框內灰色提示文本效果

 

Html代碼  收藏代碼
  1. <html>    
  2. <head>    
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
  4. <title>input test</title>    
  5. <script type="text/javascript"  
  6.     src="./js/jquery.js"></script>  
  7. <script type="text/javascript">    
  8. function inputTipText(){      
  9. $("input[class*=grayTips]") //所有樣式名中含有grayTips的input     
  10. .each(function(){     
  11.    var oldVal=$(this).val();   //默認的提示性文本     
  12.    $(this)     
  13.    .css({"color":"#888"})  //灰色     
  14.    .focus(function(){     
  15.     if($(this).val()!=oldVal){$(this).css({"color":"#000"})}else{$(this).val("").css({"color":"#888"})}     
  16.    })     
  17.    .blur(function(){     
  18.     if($(this).val()==""){$(this).val(oldVal).css({"color":"#888"})}     
  19.    })     
  20.    .keydown(function(){$(this).css({"color":"#000"})})     
  21.        
  22. })     
  23. }     
  24.     
  25. $(function(){     
  26. inputTipText();  //直接調用就OK了     
  27. })     
  28. </script>    
  29. </head>    
  30. <body>    
  31. <input type="text" value="輸入您的用戶名" class="grayTips" />    
  32. <input type="text" value="輸入您的登錄密碼" class="aaagrayTips"/>    
  33. </body>    
  34. </html>  
  35. 二、input標籤獲取焦點是文本框內提示信息清空   
  36. <html>  
  37. <head>  
  38. <title></title>  
  39. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">  
  40. </head>  
  41. <script language="javascript" type="text/javascript">   
  42. function addLoadEvent(func){  
  43.     var oldonload = window.onload;  
  44.     if (typeof window.onload != 'function'){  
  45.         window.onload = func;  
  46.     }else{  
  47.         window.onload = function(){  
  48.             oldonload();  
  49.             func();  
  50.         }  
  51.     }  
  52. }  
  53. function checkText(){  
  54.     var textId = document.getElementById('test');  
  55.         //設置文本框中的字體顏色爲灰色  
  56.         document.getElementById('test').style.color='#C0C0C0';  
  57.     var textDefault = '中文';  
  58.     function cls(){  
  59.         if (this.value == textDefault){  
  60.             this.value = '';  
  61.         }  
  62.     }  
  63.     function res(){  
  64.         if (this.value == ''){  
  65.             this.value = textDefault;  
  66.         }  
  67.     }  
  68.     textId.onfocus = cls;  
  69.     textId.onblur = res;  
  70. }  
  71. addLoadEvent (checkText);  
  72. </script>  
  73. <body>  
  74. <input type="text" id="test" value="中文" />  
  75. </body>  
  76. </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章