前言:本人是新生小白,JavaScript学的很菜,如有错误之处欢迎指出。
目录
2.然后我们定义一个密码输入框和小眼睛图片(这里我用到是img,也可以用background)
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- </head>
- <body>
- </body>
- </html>
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <style>
- img{
- width: 20px;
- height: 20px;
- position: absolute;
- left: 180px;
- top: 3px;
- }
-
- div{
- position: relative;
- }
-
- input{
- width: 200px;
- }
- </style>
- </head>
- <body>
- <div>
- <input type="password" name="" id="">
- <img src="./by.png" alt="">
- </div>
-
-
- </body>
- </html>
在这里我们提前给input和img设置上基本的css样式
-
-
- <script>
- var btn = document.querySelector('input');
- var imgs = document.querySelector('img');
-
-
- </script>
- <script>
- var btn = document.querySelector('input');
- var imgs = document.querySelector('img');
- var flag = true;
- imgs.onclick = function(){
-
-
- }
-
- </script>
3.使用if语句添加判断条件
- <script>
- var btn = document.querySelector('input');
- var imgs = document.querySelector('img');
- var flag = true;
- imgs.onclick = function(){
-
-
- if(flag){
- imgs.src = './icon_2.png';
- btn.type = 'text';
- }else{
- imgs.src = './by.png';
- btn.type = 'password';
- }
- flag=!flag
- }
-
- </script>
小提醒:input type="password" 的type有多个类型,password是密码,会把输入的内容以点的形式展现出来,text则就是输入的内容
目录
2.然后我们定义一个密码输入框和小眼睛图片(这里我用到是img,也可以用background)
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <title></title>
- <style>
- img{
- width: 20px;
- height: 20px;
- position: absolute;
- left: 180px;
- top: 3px;
- }
-
- div{
- position: relative;
- }
-
- input{
- width: 200px;
- }
- </style>
- </head>
- <body>
- <div>
- <input type="password" name="" id="">
- <img src="./by.png" alt="">
- </div>
-
- <script>
- var btn = document.querySelector('input');
- var imgs = document.querySelector('img');
- var flag = true;
- imgs.onclick = function(){
-
-
- if(flag){
- imgs.src = './icon_2.png';
- btn.type = 'text';
- }else{
- imgs.src = './by.png';
- btn.type = 'password';
- }
- flag=!flag
- }
-
- </script>
- </body>
- </html>