摘要:用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>
*{
margin: 0;
padding: 0;
box-sizing: border-box;
}
.big{
width: 200px;
height: 31px;
margin: 20px auto;
border-bottom: 1px solid hsl(351, 3%, 49%);
position: relative;
}
.big input{
width: 150px;
height: 30px;
border: 0;
outline: 0;
}
.big img{
width: 30px;
height: 15px;
position: absolute;
top: 10px;
right: 5px;
}
</style>
</head>
<body>
<div class="big">
<input type="password" id="input">
<img src="images/close.png" alt="" id="img">
</div>
<script>
var input=document.getElementById('input');
var img=document.getElementById('img');
var i=0;
img.onclick=function(){
if(i==0){
img.src='images/open.png';
input.type='text';
i=1;
} else{
img.src='images/close.png';
input.type='password';
i=0;
}
}
</script>
</body>
</html>