题目:可以通过点击按钮查看或隐藏密码
分析:
1. 核心思路:点击眼睛按钮,把密码类型改为文本框就可以看见里面的密码;
2. 一个按钮两个状态,点击一次,切换为文本框,继续点击一次切换为密码框;
3. 算法:利用一个flag变量,来判断flag的值,如果是1就切换为文本框,flag设置为0,如果是0就切换为密码框,flag设置为1。
<!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 #ccc;
margin: 100px auto;
position: relative;
}
.box input {
width: 370px;
height: 30px;
border: 0px;
outline: none;
}
.box img {
width: 24px;
position: absolute;
top: 2px;
right: 2px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="./images/close.jpg" alt="" id="eye">
</label>
<!-- input:password -->
<input type="password" name="" id="pwd">
</div>
<script>
var eye = document.getElementById('eye');
var pwd = document.getElementById('pwd');
var flag = 0;
eye.onclick = function () {
if (flag == 0) {
pwd.type = 'text';
flag = 1;
eye.src = './images/open.jpg ';
} else {
pwd.type = 'password';
flag = 0;
eye.src = './images/close.jpg';
}
}
</script>
</body>
</html>