<!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: 1px solid #ccc;
margin: 100px auto;
position: relative;
}
.box input {
width: 370px;
height: 30px;
border: 0;
outline: none;
}
input::-ms-reveal {
display: none;
}
.box img {
width: 24px;
position: absolute;
top: 8px;
right: 8px;
}
</style>
</head>
<body>
<div class="box">
<label for="">
<img src="https://p3.ssl.qhimgs1.com/sdr/400__/t01f88d0ac7b0883ab3.png" alt="" id="eye">
</label>
<input type="password" id="pwd">
</div>
<script>
//1.获取元素
var eye = document.getElementById('eye');
var pwd = document.getElementById('pwd');
//2.绑定事件 处理程序
var flag = 0;
eye.onclick = function(){
//点击一次之后 flag一定要变化
if(flag == 0){
pwd.type = 'text';
eye.src = 'https://img1.baidu.com/it/u=2531668931,3291611039&fm=253&fmt=auto&app=138&f=JPEG?w=489&h=500';
flag = 1
}
else{
pwd.type = 'password';
eye.src = "https://p3.ssl.qhimgs1.com/sdr/400__/t01f88d0ac7b0883ab3.png";
flag = 0
}
}
</script>
</body>
</html>
JavaScript中,仿京东显示隐藏密码
最新推荐文章于 2024-03-11 18:00:14 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)