<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.box {
width: 400px;
border: 1px solid red;
margin: 100px auto;
position: relative;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box img {
position: absolute;
top: 3px;
right: 3px;
width: 24px;
height: 24px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="./img/1.png" alt="" id="eye">
</label>
<input type="password" id="psd">
</div>
<script>
//获取元素
var eye = document.getElementById('eye');
var pwd = document.getElementById('psd');
//注册事件
var flag = 0;
eye.onclick = function() {
//点击榆次等于变换
if (flag == 0) {
pwd.type = 'text';
flag = 1;
eye.src = './img/2.png';
} else {
pwd.type = 'password';
flag = 0;
eye.src = './img/1.png';
}
}
</script>
</body>
</html>
运行结果