案例-------显示隐藏密码明文

<!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>
      /* 清除内外边距 */
      * {
        margin: 0;
        padding: 0;
      }

      .box {
        /* 水平居中 */
        margin: 100px auto;

        width: 400px;
        /* height: 50px; */
        border-bottom: 1px solid #ccc;
        /* background-color: antiquewhite; */
        position: relative;
        /* 子绝父相 */
      }
      .box input {
        width: 270px;
        height: 30px;
        border: 0;
        outline: none;
        /* 轮廓线改成none */
        /* position: absolute; */
        /* margin-top: 10px; */
        /* margin-left: 20px; */
      }
      .box img {
        position: absolute;
        top: 2px;
        right: 2px;
        /* float: right; */
        width: 24px;
        /* height: 10px; */
      }
    </style>
  </head>
  <body>
    <!-- 明文密码 -->
    <!-- 核心思想:点击眼睛的时候,密码框会变成文本框 -->
    <!-- 先把CSS样式写完 -->
    <!-- 首先有一个大的盒子,里面有一个小的盒子和一个a标签 -->
    <div class="box">
      <input type="password" id="pwd" />
      <label for="">
        <img src="./imgs/close.png" alt="" id="eye" />
      </label>
      <!-- label经常和input在一起使用 -->
    </div>
    <script>
      // 1.获取元素
      var eye = document.getElementById("eye");
      var pwd = document.getElementById("pwd");
      //   2.注册事件
      // 点击闭眼睛的时候就改成文本框
      var flag = 0;
      eye.onclick = function () {
        if (flag == 0) {
            // 判断
          pwd.type = "text";
          eye.src = "./imgs/open.png";
          flag = 1;
        //   重新赋值为1
          //   点击一次之后一定要变化
        } else {
          pwd.type = "password";
          eye.src = "./imgs/close.png";
          flag = 0;
          //再变回去
        }
      };
      //   算法:利用一个flag变量
    </script>
  </body>
</html>

posted @   missSherry1014  阅读(202)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
点击右上角即可分享
微信分享提示