设计一个变量的切换来实现
实例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box{ width:300px; margin:50px auto; } #span{ display:inline-block; width: 60px; background-color: lightcoral; text-align: center; height:20px; line-height: 20px; color: whitesmoke; border:1px solid lightcoral; font-weight: bold; } input{ width:150px; height:20px; } .login{ width: 300px; margin-left:60px; } </style> </head> <body> <div class="box"> <p> <label for="username">账号</label> <input type="text" name="" id="username" autofocus placeholder="请输入8-12位"> </p> <p> <label for="password">密码</label> <input type="password" name="" id="password" placeholder="别输错了哦"> <span id="span">show</span> </p> <p class="login"> <button>登录</button> <button>忘记密码</button> </p> </div> <script> var passwd = document.getElementById('password'); var span = document.getElementById('span'); var flag = 0; span.onclick=function (event) { if (flag === 0){ this.innerHTML = 'hidden'; passwd.type = 'text'; flag = 1; }else { this.innerHTML = 'show'; passwd.type = 'password'; flag = 0; } } </script> </body> </html>
运行实例 »
点击 "运行实例" 按钮查看在线实例