<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0"
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<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: 9px;
right: 2px;
width: 24px;
}
</style>
</head>
<body>
<div class='box'>
<label for=''>
<img src="images/close.jpg" alt="" id="eye">
</label>
<input type='password' name='' id="pwd">
</div>
<script>
//1.获取元素 获取图片和密码 改成睁眼和文本
var eye=document.getElementById('eye');
var pwd=document.getElementById('pwd');
//注册事件 处理程序
var flag=0;
eye.onclick=function(){
//点击一次之后flag要变化
if (flag == 0) {
pwd.type = 'text';
eye.src='images/open.jpg';
flag = 1; //赋值操作
}else {
pwd.type='password';
eye.src='images/close.jpg';
flag=0;
}
}
</script>
</body>
</html>
8.案例:显示隐藏密码
最新推荐文章于 2022-10-05 20:17:15 发布