点击右边的眼睛,显示或隐藏密码
效果图:
右边的小眼睛闭着,不显示密码
小眼睛图标睁开,显示密码
代码:
<div class="box">
<label for="">
<img src="images/close.png" id="eye" />
</label>
<input type="password" name="" id="pwd">
</div>
css:
<style>
.box {
position: relative;
width: 400px;
margin: 100px auto;
border-bottom: 1px solid #ccc;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box img {
position: absolute;
right: 2px;
top: 2px;
width: 24px;
height: 24px;
}
</style>
js代码:
<script>
//1.获取元素,图片和密码
var eye = document.getElementById('eye');
var pwd = document.getElementById('pwd');
//2.注册事件
var flag = 0;
eye.onclick = function () {
if (flag == 0) {
pwd.type = 'text';
eye.src = 'images/open.png';
flag = 1;
} else {
pwd.type = 'password';
eye.src = 'images/close.png';
flag = 0;
}
}
</script>