【JavaScript练习一】登录页面显示隐藏密码

文章详细介绍了如何使用JavaScript在密码登录页面中,通过点击事件控制密码输入框的显示和隐藏,以及使用``标签切换密码图标。
摘要由CSDN通过智能技术生成

一、密码登录页面

在这里插入图片描述

1.完整代码如下:

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        h3 {
            text-align: center;
            margin: 10px 0;
        }

        .box-account {
            width: 400px;
            height: 36px;
            line-height: 32px;
            border: 1px solid #ccc;
            margin: 40px auto;
        }

        .box-account input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }

        .box-pwd {
            position: relative;
            width: 400px;
            height: 36px;
            line-height: 32px;
            border: 1px solid #ccc;
            margin: 40px auto;
        }

        .box-pwd input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }

        /* 去除游览器默认的密码显示图标 */
        input[type="password"]::-ms-reveal {
            display: none;
        }

        .box-pwd img {
            position: absolute;
            top: 5px;
            right: 2px;
            width: 24px;
        }

        button {
            display: block;
            width: 200px;
            height: 36px;
            line-height: 36px;
            border: 0;
            background: #ccc;
            font-size: 14px;
            margin: 0 auto;
        }
    </style>
</head>

<body>
    <h3>密码登录</h3>
    <!-- 账号框 -->
    <div class="box-account">
        <input type="text" name="" id="account">
    </div>
    <!-- 密码框 -->
    <div class="box-pwd">
        <label for="">
            <img src="./images/隐藏密码.png" alt="" id="eye">
        </label>
        <input type="password" name="" id="pwd">
    </div>
    <!-- 登录按键 -->
    <button type="submit">登&nbsp&nbsp&nbsp录</button>
    <script>
        // 获取元素
        let eye = document.getElementById('eye');
        let pwd = document.getElementById('pwd');
        // 注册事件 处理程序
        let flag = 0;
        eye.onclick = function () {
            // 点击一次后 flag变化
            if (flag == 0) {
                pwd.type = 'text';
                eye.src = "./images/显示.png";
                flag = 1;
            } else {
                pwd.type = 'password';
                eye.src = "./images/隐藏密码.png";
                flag = 0;
            }
        }
    </script>
</body>

</html>

2.JavaScript代码如下:

<script>
        // 获取元素
        let eye = document.getElementById('eye');
        let pwd = document.getElementById('pwd');
        // 注册事件 处理程序
        let flag = 0;
        eye.onclick = function () {
            // 点击一次后 flag变化
            if (flag == 0) {
                pwd.type = 'text';
                eye.src = "./images/显示.png";
                flag = 1;
            } else {
                pwd.type = 'password';
                eye.src = "./images/隐藏密码.png";
                flag = 0;
            }
        }
    </script>

二、总结

JS代码的作用是在当前作用域中创建一个名为flag的变量,并将其初始化为数值0,然后可以通过改变flag的值0,1来表示。
点击一次eye图标,当flag=0时,输入框类型为文本框,图标为显示.png;然后把1赋值给flag,输入框类型为密码框,图标为隐藏密码.png,然后把0赋值给flag,进入循环。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值