显示隐藏密码–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>
<link rel="stylesheet" href="./font_2368962_3yxu32fsuq3/iconfont.css">
<style>
.box{
width: 400px;
border-bottom: 1px solid #ccc;
margin: 100px auto;
}
.box input{
width: 370px;
height: 30px;
border: 0;
outline: none;
}
</style>
</head>
<body>
<div class="box">
<label for=""></label>
<input type="password" name="" id="pwd">
<span class="iconfont" id="ico"></span>
</div>
<script>
//1.获取元素
var eye = document.getElementById('ico');
var pwd = document.getElementById('pwd');
//2.注册事件 处理程序
var flag = 0;
ico.onclick = function(){
if(flag == 0)
{
pwd.type = 'text';
ico.innerHTML = '';
flag = 1;
}else{
pwd.type = 'password';
ico.innerHTML = '';
flag = 0;
}
}
</script>
</body>
</html>
利用点击事件,修改input的输入方式,同时根据flag判断是隐藏还是显示