模仿输入显示和隐藏密码
效果展示
方法如下
代码图片
复制如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
div {
width: 380px;
margin: 50px auto;
border-bottom: 1px solid #ccc;
position: relative;
padding-left: 20px;
}
input {
width: 370px;
border: none;
outline: none;
}
img {
width: 24px;
position: absolute;
top: -3px;
right: 10px;
}
</style>
</head>
<body>
<div>
<input type="password" id="pwd">
<label><img src="images/close.png" id="img"></label>
</div>
</body>
<script type="text/javascript">
var flag=0;
document.getElementById('img').onclick=function(){
if(flag==0){
this.src="images/open.png";
document.getElementById('pwd').type="text";
flag=1;
}
else{
this.src="images/close.png";
document.getElementById('pwd').type="password";
flag=0;
}
}
</script>
</html>