<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> </head> <body> <style> .father { position: relative; width: 300px; height: 40px; } input { width: 300px; height: 40px; } span { position: absolute; top: 9px; right: 3px; } </style> <div class="father"> <input type="password" name="pwd" id="pwd" /> <span><img src="img/小人头.png" width="24px" height="24px"></span> </div> <script> /* * (1)核心思路:点击眼睛按钮,把密码框类型改为文本框就可以看见里面的密码 * (2)一个按钮两个状态,点击一次,切换为文本框,继续点击一次切换为密码框架 * (3)算法:利用一个flag变量,设置为0,来判断flag的值,如果是0,就切换为密码框,如果是 1,就切换为文本框, */ //获取事件源 var input=document.querySelector("#pwd"); var img=document.querySelector("img"); var flag=0; img.onclick=function(){ if(flag==1){ input.type="password"; img.src="img/小人头.png"; flag=0; } else { input.type="text"; img.src="img/小设置.png"; flag=1; } } </script> </body></html>