js-仿京东显示隐藏密码

<!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">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 300px;
            height: 30px;
            border-bottom: 1px solid #ccc;
        }
        
        .box input {
            border: 0;
            width: 270px;
            outline: none;
        }
        
        .box img {
            position: absolute;
            top: 2px;
            right: 2px;
        }
    </style>
</head>

<body>
    <div class="box">
        <input type="password" class="ipt">
        <img src="eye_bi.png">
    </div>
    <script>
        var inpt = document.querySelector(input);
        var eye = document.querySelector(img);
        var flag = 0;
        eye.onclick = function() {
            if (flag == 0) {
                eye.src = eye_open.png;
                inpt.type = text;
                flag = 1;
            } else {
                inpt.type = password;
                eye.src = eye_bi.png
                flag = 0;
            }
        }
    </script>
</body>

</html>
经验分享 程序员 微信小程序 职场和发展