思路: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" />