JS控制文本框中的密码显示/隐藏功能

时间:2022-07-21 21:44:12
 <html>
<head>
<title>【荐】JS控制文本框中的密码显示/隐藏功能_网页代码站(www.6a8a.com)</title>
<style>
body,input{font:menu}
</style>
</head>
<body>
<form method="POST" action="addcheck.asp" name="forms">
<span id=box><input type="password" name="password" size="20" value="www.6a8a.com"></span>
<span id=click><a href="javascript:ps()">显示密码</a></span>
<script language="JavaScript">
function ps(){
if (this.forms.password.type="password")
box.innerHTML="<input type=\"html\" name=\"password\" size=\"20\" value="+this.forms.password.value+">";
click.innerHTML="<a href=\"javascript:txt()\">隐藏密码</a>"}
function txt(){
if (this.forms.password.type="text")
box.innerHTML="<input type=\"password\" name=\"password\" size=\"20\" value="+this.forms.password.value+">";
click.innerHTML="<a href=\"javascript:ps()\">显示密码</a>"}
</script></form>
<br />
<p><a href="http://www.6a8a.com">网页代码站</a> - 最专业的网页代码下载网站 - 致力为中国站长提供有质量的网页代码!</p>
</body>
</html>

另一种写法:

<dd>
<em>新&nbsp;密&nbsp;码</em>
<label>
<span class="placeholder">请输入6-16位的新密码</span>
<input id="pwd" type="password" class="input_txt" >
<a class="input_eye" id="clc" href="javascript:;"> </a>
<p class="error_msg">密码xxxx错误</p>
</label> <script language="JavaScript">
var pwd = document.querySelector('#pwd');
document.querySelector('#clc').addEventListener('click', function() {
showPwd(pwd)
}, false); function showPwd(obj) {
var type = obj.getAttribute('type');
if (type == 'password') {
obj.setAttribute('type', 'text');
} else {
obj.setAttribute('type', 'password');
}
}
</script> </dd>