本文實例講述了JS實現密碼框根據焦點的獲取與失去控制文字的消失與顯示效果。分享給大家供大家參考,具體如下:
思路:
1、首先用把密碼框用txt暫時替代,并賦上默認值 <input type="text" value="請輸入密碼" />
2、當文本框獲取焦點后,把默認值清空,把type改為password。
3、當文本框失去焦點后,把type改為txt,把默認值設為“請輸入密碼”。
JS代碼:
window.onload=function(){ var input=document.getElementById('input'); input.onfocus=function(){ if(this.value=='請輸入密碼'){ this.value=''; this.type='password'; }; }; input.onblur=function(){ if(!this.value){ this.type = 'text'; this.value = '請輸入密碼'; }; };};
HTML代碼:
復制代碼代碼如下:
<input type="text" value="請輸入密碼" id="input" />
希望本文所述對大家JavaScript程序設計有所幫助。