Adam____ 2023-02-02 17:23 采纳率: 75%
浏览 37
已结题

关于#前端#的问题,如何解决?

前端练习
密码输入栏,实现密码能够实现可见与不可见。
打出来后无法实现功能,请问哪里出问题了呢?

<!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 {
            position: relative;
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 100px auto;
        }
        
        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }
        
        .box img {
            position: absolute;
            width: 24px;
            top: 2px;
            right: 2px;
        }
    </style>
</head>

<body>
    <div class="box">
        <label for='' id="lbl">
        <img src="..\Dom\close.png" alt="" id="eye">
    </label>
        <input type="password" name="" id="psw">
    </div>
    <script>
        var eye = document.getElementById('eye');
        var psw = document.getElementById('psw');
        var lbl = document.getElementById('lbl');
        var flag = 0;
        eye.onclick = function() {
            if (flag = 0) {
                psw.type = 'text';
                flag = 1;
            } else {
                psw.type = 'password';
                flag = 0;
            }
        }
    </script>
</body>

</html>

  • 写回答

2条回答 默认 最新

  • 简效 2023-02-02 17:30
    关注

    if判断的括号里要写 == 或者 === 才可以进行判断 ,给你修改的代码如下,有用的话采纳一下吧

    <!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 {
                position: relative;
                width: 400px;
                border-bottom: 1px solid #ccc;
                margin: 100px auto;
            }
            
            .box input {
                width: 370px;
                height: 30px;
                border: 0;
                outline: none;
            }
            
            .box img {
                position: absolute;
                width: 24px;
                top: 2px;
                right: 2px;
            }
        </style>
    </head>
     
    <body>
        <div class="box">
            <label for='' id="lbl">
            <img src="..\Dom\close.png" alt="" id="eye">
        </label>
            <input type="password" name="" id="psw">
        </div>
        <script>
            var eye = document.getElementById('eye');
            var psw = document.getElementById('psw');
            var lbl = document.getElementById('lbl');
            var flag = 0;
            eye.onclick = function() {
                if (flag === 0) {
                    psw.type = 'text';
                    flag = 1;
                } else {
                    psw.type = 'password';
                    flag = 0;
                }
            }
        </script>
    </body>
     
    </html>
     
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月10日
  • 已采纳回答 2月2日
  • 创建了问题 2月2日