js实现,输入的密码点击小眼睛显示和带点状
<!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 {
position: relative;
width: 400px;
border-bottom: 1px solid #ccc;
margin: 100px auto;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box img {
position: absolute;
top: 2px;
right: 2px;
width: 24px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="./imgs/no-f .png" alt="" id="eye">
</label>
<input type="password" name="" id="pwd">
</div>
<script>
var eye = document.getElementById("eye");
var pwd = document.getElementById("pwd");
var flag = 0;//最初的状态
// pwd.type = 'text';的时候,可以看到输入的密码
eye.onclick = function () {
// 点击一次之后,flag要发生变化
if (flag == 0) {
pwd.type = 'text';
eye.src = './imgs/ok.png';
flag = 1;
} else {
pwd.type = 'password';
eye.src = './imgs/no-f .png';
flag = 0;
}
}
</script>
</body>
</html>
结果截图:
点击红色小叉后:
再点击绿色小叉又变成红色小叉的样式