输入显示隐藏按钮案例

<style>

        .register {

            width: 161px;

            position: relative;

        }


        input {

            outline: none;

            border: 0;

            border-bottom: 1px solid #ccc;

        }


        label img {

            width: 15px;

            position: absolute;

            right: 4px;

            top: 4px;

        }

    </style>

</head>

<body>

    <div class="register">

        <label for="">

            <img src="./img/不显示密码.png" alt="" id="see">

        </label>

        <input type="password" name="" id="psw">

    </div>

</body>

<script>

    var images = document.getElementById('see');

    var password = document.getElementById('psw');

    var flag = 0;

    images.onclick = function() {

        if (flag == 0) {

            password.type = 'text';

            flag = 1;

            images.src = "./img/显示.png";

        } else {

            password.type = 'password';

            flag = 0;

            images.src = "./img/不显示密码.png";

        }

    }

</script>

<!-- //核心算法:利用一个flag变量,来判断flag的值,如果是1就切换文本框,flag设置为0,如果是0就切换为密码框,flag设置为1. -->

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容