<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 400px;
border-bottom: 1px solid gray;
margin: 100px auto;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box img {
position: absolute;
width: 20px;
right: 450px;
top: 110px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="../../img/闭眼.png" alt=""id="eye">
</label>
<input type="password" name="" id="pwd">
</div>
<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 = '../../img/睁眼.png';
flag = 1;
} else {
pwd.type = 'password'
eye.src = '../../img/闭眼.png';
flag = 0; //复原
}
}
</script>
</body>
</html>