如图所示,想要实现下边的功能怎么办?其实很简单。。。
HTML代码如下(注意:需要给input和img各取一个id,并且在css中把样式定位好)
<input type="password" class="form-control" id="oldpassword" placeholder="请输入旧密码">
<img src="../../img/showpassword.png" alt="" id="show" onclick="hideShowPsw() ">
JS代码如下(要准备好俩张图片,分别是显示密码,隐藏密码)
<script type="text/javascript">
//获取input框内的切换图片id和input文本框的id
var img = document.getElementById("show");
var input = document.getElementById("oldpassword");
function hideShowPsw() {
if (input.type == "password") {
input.type = "text";
img.src ="../../img/showpassword.png";
} else {
input.type = "password";
img.src = "../../img/closepassword.png";
}
}
</script>
彩蛋: