效果图
所需图片
html部分
<input type="text">
<img src="./images/eye.png" alt="">
js部分
<script>
let _input = document.querySelector("input")
let _img = document.querySelector("img")
let a = 0
_img.onclick = function () {
if (a == 0) {
_input.type = 'password'
_img.src = './images/lock.png'
a++
} else {
_input.type = 'text'
_img.src = './images/eye.png'
a--
}
}
</script>
以上就是全部代码。