1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <meta charset="UTF-8">
    5. <title></title>
    6. </head>
    7. <body>
    8. <style>
    9. .father {
    10. position: relative;
    11. width: 300px;
    12. height: 40px;
    13. }
    14. input {
    15. width: 300px;
    16. height: 40px;
    17. }
    18. span {
    19. position: absolute;
    20. top: 9px;
    21. right: 3px;
    22. }
    23. </style>
    24. <div class="father">
    25. <input type="password" name="pwd" id="pwd" />
    26. <span><img src="img/小人头.png" width="24px" height="24px"></span>
    27. </div>
    28. <script>
    29. /*
    30. * (1)核心思路:点击眼睛按钮,把密码框类型改为文本框就可以看见里面的密码
    31. * (2)一个按钮两个状态,点击一次,切换为文本框,继续点击一次切换为密码框架
    32. * (3)算法:利用一个flag变量,设置为0,来判断flag的值,如果是0,就切换为密码框,如果是 1,就切换为文本框,
    33. */
    34. //获取事件源
    35. var input=document.querySelector("#pwd");
    36. var img=document.querySelector("img");
    37. var flag=0;
    38. img.onclick=function(){
    39. if(flag==1){
    40. input.type="password";
    41. img.src="img/小人头.png";
    42. flag=0;
    43. }
    44. else {
    45. input.type="text";
    46. img.src="img/小设置.png";
    47. flag=1;
    48. }
    49. }
    50. </script>
    51. </body>
    52. </html>