一般网站都有显示和隐藏密码的功能,利用js制作一个点击元素显示/隐藏密码。
HTML
<div class="box">
<label for=""></label>
<input type="password" name="" id="pwd">
</div>
CSS
.box {
position: relative;
width: 400px;
border-bottom: 1px solid #ccc;
margin: 100px auto;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
.box label {
position: absolute;
top: 2px;
right: 2px;
width: 24px;
height: 24px;
background-color: pink;
cursor: pointer;
background: url(./images/close.png) no-repeat;
background-size: cover;
}
.box label.active {
background-image: url(./images/open.png);
}
JS
// 1. 获取元素 label 和 input
const label = document.querySelector('label')
const input = document.querySelector('input')
// 2. 给label 注册事件, 可以切换类实现图片的切换
// 声明一个变量来控制
let flag = true
label.addEventListener('click', function () {
this.classList.toggle('active')
// 3. 因为要修改input的 type属性 text和password,可以使用一个变量来控制 flag , 如果为true 就切换为text ,如果为false就修改为 password
if (flag) {
input.type = 'text'
} else {
input.type = 'password'
}
flag = !flag
})