`

js动态修改input输入框的type属性及验证Email

 
阅读更多
需要实现的效果:一个输入框,当输入框未获得焦点的时候,value 值为 “密码”;当输入框失去焦点的时候,输入内容显示为”*****”
 
Html代码 复制代码 收藏代码
  1. <input name="password" type="text" id="showPwd" tabindex="2" class="input" value="密码" />  
<input name="password" type="text" id="showPwd" tabindex="2" class="input" value="密码" />
 
我们很直接会想到下面的js

Js代码 复制代码 收藏代码
  1. $("#showPwd").focus(function(){   
  2.     $(this).attr('type','password');   
  3. });  
$("#showPwd").focus(function(){
    $(this).attr('type','password');
});
 
发现并没有实现预期效果,出现 uncaught exception type property can’t be changed 错误,查看jQuery 1.42源码 1488 行
 
Js代码 复制代码 收藏代码
  1. // We can't allow the type property to be changed (since it causes problems in IE)   
  2. if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {   
  3.     jQuery.error( "type property can't be changed" );   
  4. }  
// We can't allow the type property to be changed (since it causes problems in IE)
if ( name === "type" && rtype.test( elem.nodeName ) && elem.parentNode ) {
    jQuery.error( "type property can't be changed" );
}
 
jQuery 修改不了用源生的JS呢?
 

Js代码 复制代码 收藏代码
  1. $("#pwd").focus(function(){   
  2.     $("#pwd")[0].type = 'password';   
  3.     $("#pwd").val("");   
  4. });  
$("#pwd").focus(function(){
    $("#pwd")[0].type = 'password';
    $("#pwd").val("");
});
 
发现在FF下可以修改并将密码输入框type 修改为 “password” 并将 value设置为空,而IE下却提示无法得到type属性,不支持该命令。 弹出 type 看看真的无法得到吗?
 

Js代码 复制代码 收藏代码
  1. $("#showPwd").focus(function(){   
  2.     alert($("#showPwd")[0].type);   
  3.     $("#showPwd")[0].type = 'password';   
  4.     $("#showPwd").val("");   
  5. });  
$("#showPwd").focus(function(){
    alert($("#showPwd")[0].type);
    $("#showPwd")[0].type = 'password';
    $("#showPwd").val("");
});
 
发现弹出text ,原来不是无法得到,只是IE下不能修改。 因此,我们想到可以先remove然后再生成一个type是password的密码输入框。
 
下面type为password的输入框
 
Html代码 复制代码 收藏代码
  1. <input name="password" type="password" id="password" class="input" style="display: none;" />  
<input name="password" type="password" id="password" class="input" style="display: none;" />
 
Js代码 复制代码 收藏代码
  1. $("#showPwd").focus(function() {   
  2.         var text_value = $(this).val();   
  3.         if (text_value == this.defaultValue) {   
  4.             $("#showPwd").hide();   
  5.             $("#password").show().focus();   
  6.         }   
  7.     });   
  8.     $("#password").blur(function() {   
  9.         var text_value = $(this).val();   
  10.         if (text_value == "") {   
  11.             $("#showPwd").show();   
  12.             $("#password").hide();   
  13.         }   
  14.     });  
$("#showPwd").focus(function() {
		var text_value = $(this).val();
		if (text_value == this.defaultValue) {
			$("#showPwd").hide();
			$("#password").show().focus();
		}
	});
	$("#password").blur(function() {
		var text_value = $(this).val();
		if (text_value == "") {
			$("#showPwd").show();
			$("#password").hide();
		}
	});
 

最终效果: 当输入框获得焦点的时,输入的内容显示为“****”;当失去焦点的时,内容为空时显示“密码”。

 

 

 

实例代码:

<script type="text/javascript">
  function test(){
     $("#showPwd").focus(function() {  
        var text_value = $(this).val();  
        var defv = this.defaultValue;
       
       if (text_value == this.defaultValue) {
            $("#showPwd").hide();
            $("#password").show().focus();
        }
    });
    $("#password").blur(function() {
        var text_value = $(this).val();
        if (text_value == "") {
            $("#showPwd").show();
            $("#password").hide();
        }
    });
  }
 
  function checkValue(obj){
     if(obj.value==''){
       obj.style.display='none';
       document.getElementById('repassword_f').style.display='';
      
     }
  }
</script>

 

密码3:<input name="password" type="text" id="showPwd" tabindex="2" class="input" value="请输入密码" />
   <input name="password" type="password" id="password" class="input" style="display: none;" /> 

 

<br/><br/>
密码5:<input type="text" id="repassword_f" value="请输入密码" onclick="this.style.display='none';document.getElementById('repassword').style.display='';document.getElementById('repassword').focus();"/>
<input name="repassword" type="password" id="repassword" onblur="checkValue(this);" style="display:none"/>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics