js实现隐藏显示密码文本框

思路:

点击显示按钮,将密码框属性修改为文本框,即可看见密码。再次点击按钮切换为密码框。

实现:设置flag变量,通过对flag值的判断,切换属性。


HTML代码

    <h2>账号登录</h2>
    <div class="box">
        <input type="text" value="" placeholder="手机号登陆">
    </div>
    <div class="box pass">
        <label for="">
            <img src="imgs/close.PNG" id="eye-slash" class="eye-slash">
        </label>

        <p class="forget">忘记密码</p>
        </label>
        <input type="password" name="" id="pwd">
    </div>

css布局

        * {
            border: 0;
            margin: 0;
            box-sizing: border-box;
        }
        h2 {
            margin: 5px auto;
            text-align: center;
        }
        .box {
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 10px auto;


        }
        .box input {
            width: 290px;
            height: 30px;
            margin: 5px;
            border: 0;
            outline: none;
            /*祛除蓝色边框*/
        }
        .pass {
            position: relative;
        }
        .eye-slash {
            position: absolute;
            bottom: 12px;
            right: 80px;

            height: 25px;
            border-right: 1px solid;
            padding-right: 8px;
        }
        .forget {
            position: absolute;
            right: 5px;
            bottom: 15px;
            color: gray;

        }

js代码

<script>
        //1.获取元素
        var eye = document.getElementById('eye-slash');
        var pwd = document.getElementById('pwd');
        //2.绑定事件
        var flag = 0;
        eye.onclick = function () {
            if (flag == 0) {
                pwd.type = 'text';
                eye.src = 'imgs/on.PNG';
                flag = 1;
            } else {
                pwd.type = 'password';
                eye.src = 'imgs/close.PNG';
                flag = 0;
            }

        }
    </script>

效果图

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值