如何实现在密码框如出现提示语

如何实现在密码框如出现提示语:
有时候我们需要在登陆表单有一些提示语言,比如“请输入用户民”和“请输入密码”等语言,至于用户名好说,但是在 密码框中出现“请输入密码”这样的语言就有点麻烦了,因为在密码框输入的内容不会以明码显示。如果动态的控制type属性的话就有兼容性问题,如果 input已经存在于页面中的话,在IE8和IE8以下浏览器中,type属性是只读的。所以就得想其他办法,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>蚂蚁部落</title>
<style type="text/css">
#tx{
  width:100px;
}
#pwd{
  display:none;
  width:100px;
}
</style>
<script type="text/javascript">
window.οnlοad=function(){
  var tx=document.getElementById("tx");
  var pwd=document.getElementById("pwd");
  tx.οnfοcus=function(){
    if(this.value!="密码")
    return;
    this.style.display="none";
    pwd.style.display="block";
    pwd.value="";
    pwd.focus();
  }
  pwd.οnblur=function(){
    if(this.value!=""){
      return;
    }
    this.style.display="none";
    tx.style.display="";
    tx.value="密码";
  }
}
</script>
</head>
<body>
<input type="text" value="密码" id="tx"/>
<input type="password" id="pwd" />
</body>

</html>

以上代码实现了我们的要求,可以出现明码的提示,当输入密码的时候就是以密码方式输入。
实现的原理非常的简单,在默认状态以type="text"文本框显示,当点击文本框的时候,以type="password"密码框显示,原来显示的文本框隐藏,也就是说做了一个替换而已。

第二种方法:

<! doctype html>
< html >
< head >
< meta charset="utf-8">
< title >input输入提示</ title >
< style >
ul{ list-style:none;}
.gray{ border:2px solid gray;}
</ style >
</ head >
 
< body >
< ul >
     < li >姓名:< input type="text" id="username" value="请输入姓名" style="color:#CCC;"/></ li >
     < li >密码:< input type="password" id="pw"/></ li >
</ ul >
< script type="text/javascript">
var username=document.getElementById("username");
var pw=document.getElementById("pw");
 
username.onfocus=function(){//onfocus 获得焦点
     this.className="gray";
     this.value="";
}
username.onblur=function(){//onblur 失去焦点
     this.className="";
     this.value="请输入姓名";
}
pw.onfocus=function(){
     this.className="gray";
     this.value="";
}
pw.onblur=function(){
     this.className="";
     this.style.type="text";
     this.value="请输入密码";
     
}
</ script >
</ body >
</ html >
 

转载于:https://www.cnblogs.com/momox/p/5090672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值