网上看到也有很多事例,自己实际操作了一下。
主要就是操作input的type属性。
此处我使用的是jquery来操作。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<title></title>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var eyeclose = $('.eyeclose');
var eye = $('.eye');
var password = $('#password');
eyeclose.click(function(){
eye.show();
eyeclose.hide();
password.attr("type","password");
});
eye.click(function(){
eye.hide();
eyeclose.show();
password.attr("type","text");
});
});
</script>
<style type="text/css">
#password{
width: 200px;
height: 35px;
outline: none;
border: 1px solid blue;
}
.eyeclose{
display: none;
cursor: pointer;
}
.eye{
cursor: pointer;
}
</style>
</head>
<body>
<div class="box">
<input type="password" name="password" id="password" value=""/><br />
<img class="eyeclose" src="img/eyeclose.svg"/><img class="eye" src="img/eye.svg" />
</div>
</body>
</html>