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