1. 程式人生 > 其它 >密碼的顯示與隱藏

密碼的顯示與隱藏

技術標籤:js功能案例js

點選右邊的眼睛,顯示或隱藏密碼
效果圖:
在這裡插入圖片描述
右邊的小眼睛閉著,不顯示密碼
在這裡插入圖片描述
小眼睛圖示睜開,顯示密碼
在這裡插入圖片描述
程式碼:

 <div class="box">
        <label for="">
            <img src="images/close.png" id="eye" />
        </label>
        <input type="password" name=""
id="pwd"> </div>

css:

 <style>
        .box {
            position: relative;
            width: 400px;
            margin: 100px auto;
            border-bottom: 1px solid #ccc;
        }

        .box input {
            width: 370px;
            height: 30px;
            border:
0; outline: none; } .box img { position: absolute; right: 2px; top: 2px; width: 24px; height: 24px; } </style>

js程式碼:

 <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 = 'images/open.png'; flag = 1; } else { pwd.type = 'password'; eye.src = 'images/close.png'; flag = 0; } } </script>