案例:js实现密码的显示与隐藏

<代码演示>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .box {
            width: 400px;
            margin: 100px auto;
            border-bottom: 1px solid #333;
            position: relative;
        }

        #pwd {
            outline: none;
            /*获得焦点时 外边框 无  */
            border: 0;
            /* 外边框 无 */
            width: 370px;
            height: 30px;
            line-height: 30;
        }

        #eye {
            position: absolute;
            right: 2px;
            top: 2px;
            width: 24px;
        }
    </style>
</head>

<body>
    <div class="box">
        <input type="password" id="pwd">
        <label for="">
            <img src="https://img-blog.csdnimg.cn/20200927154818954.png" id="eye">
        </label>
    </div>
    <script>
        // 利用一个变量flag 两种状态  0 表示闭眼睛,不显示密码 ; 1 表示睁开眼睛,显示密码
        // 1.获取元素
        var eye = document.getElementById("eye");
        var input = document.querySelector("#pwd");
        // 2.注册事件 3.处理程序
        var flag = 0;
        eye.onclick=function(){
            if(flag==0){
                input.type="text"//表示此时input的属性变为输入文本,密码输入显示
                eye.src="https://img-blog.csdnimg.cn/20200927154739332.png";
                flag=1;//此时经过点击小眼睛状态变为打开 就表示此时flag的值为1 
            }else{
                input.type="password"//表示此时input的属性变为输入密码,密码输入隐藏
                eye.src="https://img-blog.csdnimg.cn/20200927154818954.png";
                flag=0;

            }
        }



    </script>
</body>

</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值