JS控制密碼框獲取焦點時文字消失,失去焦點時文字出現


思路:1、首先用把密碼框用txt暫時替代,並賦上默認值 <input type="text" value="請輸入密碼" />

         2、當文本框獲取焦點后,把默認值清空,把type改為password。

         3、當文本框失去焦點后,把type改為txt,把默認值設為“請輸入密碼”。

 

JS代碼:

 

 1 window.onload=function(){
 2 
 3   var input=document.getElementById('input');
 4 
 5   input.onfocus=function(){
 6 
 7     if(this.value=='請輸入密碼'){
 8 
 9       this.value='';
10       this.type='password';
11     };
12 
13   };
14 
15   input.onblur=function(){
16 
17     if(!this.value){
18 
19       this.type = 'text';
20       this.value = '請輸入密碼';
21     };
22   }; 
23 
24 };

 

HTML代碼:

1 <input type="text" value="請輸入密碼" id="input" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM