JS 实现密码显隐切换

简介: JS 实现密码显隐切换

今天我们要写的是一个常用的小案例,密码隐藏与显示

样式如下:


input 标签的密码框自带有隐藏密码的功能,这篇文章就当做隐藏密码的内部实现机制吧......(而且我不知道如何去除 input 自带的隐藏密码图标,有会的大佬留个言呗!!)


<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{
background-color: rgb(204, 204, 204);
       }
#divout{box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
margin-left:-216px;
margin-top: -160px;
display: inline-block;
width: 450px;
height: 180px; 
background-color: rgb(204, 204, 204);
  }
#divout #div1{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#divout #div2{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 70px;
background-color: rgb(204, 204, 204);
  }
#divout #div3{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#text1{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
#pwd{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
img{
position:absolute;
right: 19px;
top: 145px;
width: 30px;
height: 30px;
  }
</style></head><body><divid="divout"><divid="div1"><inputtype="text"id="text1"placeholder="请输入用户名"></div><divid="div2"></div><divid="div3"><label><imgsrc="./登录/密码隐藏 (2).png"alt=""id="img1"></label><inputtype="password"id="pwd"autocomplete="off"placeholder="请输入密码"></div></div><script>varpwd=document.getElementById('pwd');
varimg=document.getElementById('img1');
varflag=0;
img.onclick=function(){
if(flag==0){
pwd.type="text";
img.src="./登录/密码显示.png";
flag=1;
            }else{
pwd.type="password";
img.src="./登录/密码隐藏 (2).png";
flag=0;
            }
        }
</script></body></html>改进代码:增加了密码输入对错提示<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=device-width, initial-scale=1.0"><title>Document</title><style>body{
background-color: rgb(204, 204, 204);
       }
#divout{box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
margin-left:-216px;
margin-top: -160px;
display: inline-block;
width: 450px;
height: 180px; 
background-color: rgb(204, 204, 204);
  }
#divout #div1{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#divout #div2{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 70px;
background-color: rgb(204, 204, 204);
  }
#divout #div3{box-sizing: border-box;
display: inline-block;
width: 450px;
height: 55px;
background-color: rgb(255, 255, 255);
border-bottom:3pxsolid;
border-color: rgb(109, 109, 109) ;
background-color: rgb(204, 204, 204);
padding-left: 10px;
padding-top: 5px;
  }
#text1{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
#pwd{height: 44px;
width: 360px;
border: 0;
outline: none;
font-size: 24px;
background-color: rgb(204, 204, 204);
color:   rgb(109, 109, 109) ;
  }
img{
position:absolute;
right: 19px;
top: 145px;
width: 30px;
height: 30px;
  }
#yn{box-sizing: border-box;
position:absolute;
right: 50px;
top: 400px;
width: 400px;
height: 60px;
  }
  .message{
background:url(./登录/正确.png)  no-repeatleftcenter ;
background-size: 5%100%;
padding-left: 25px;
font-weight: bold;
color: rgb(68, 68, 68);
display: none;
  }
  .wrong{
background: url(./登录/错误空心.png) no-repeatleftcenter;
background-size: 5%100%;
color: brown;
  }
  .right{
background: url(./登录/正确.png) no-repeatleftcenter;
background-size: 5%100%;
color: rgb(6, 189, 46);
  }
</style></head><body><divid="divout"><divid="div1"><inputtype="text"id="text1"placeholder="请输入用户名"></div><divid="div2"></div><divid="div3"><label><imgsrc="./登录/密码隐藏 (2).png"alt=""id="img1"></label><inputtype="password"id="pwd"autocomplete="off"placeholder="请输入密码"></div></div><divid="yn"><pclass="message">请输入正确的6-16位的密码</p></div><script>varpwd=document.getElementById('pwd');
varimg=document.getElementById('img1');
varmes=document.querySelector('.message');
pwd.onfocus=function(){
mes.style.display='block';
        }
pwd.onblur=function(){
if(pwd.value.length<6||pwd.value.length>16){
mes.className='message wrong';
mes.innerHTML='请输入正确的符合要求的密码';
            }else{
mes.className='message right';
mes.innerHTML='符合要求'            }
        }
varflag=0;
img.onclick=function(){
if(flag==0){
pwd.type="text";
img.src="./登录/密码显示.png";
flag=1;
            }else{
pwd.type="password";
img.src="./登录/密码隐藏 (2).png";
flag=0;
            }
        }
</script></body></html>
相关文章
|
4月前
|
JavaScript 数据安全/隐私保护
JS正则验证密码
JS正则验证密码
|
3月前
|
移动开发 JavaScript 安全
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
js的常见的三种密码加密方式-MD5加密、Base64加密和解密和sha1加密详解总结
101 0
|
8月前
|
JavaScript 数据安全/隐私保护
js记住密码
js记住密码
35 0
|
4月前
|
前端开发 JavaScript 数据安全/隐私保护
使用 HTML、CSS 和 JavaScript 制作的随机密码生成器
使用 HTML、CSS 和 JavaScript 制作的随机密码生成器
83 0
|
前端开发 JavaScript 数据安全/隐私保护
react + zarm + rc-form + crypto-js 实现个人中心页面,头像上传,密码重置,登录退出功能
react + zarm + rc-form + crypto-js 实现个人中心页面,头像上传,密码重置,登录退出功能
98 0
react + zarm + rc-form + crypto-js 实现个人中心页面,头像上传,密码重置,登录退出功能
|
JavaScript 前端开发
利用JavaScript实现二级联动
利用JavaScript实现二级联动 要实现JavaScript二级联动效果,首先要确定需要哪些技术: 二维数组 for in循环 new Option(text,value,true,true) add(option,null) onchange() 表单事件 HTML代码: &lt;!-- &lt;input type=&quot;text&quot; id=&quot;text&quot;&gt; --&gt; 请选择省份: &lt;select name=&quot;&quot; id=&quot;provinces&quot;&gt; &lt;!-- &lt;option value=&quot;江苏省&quot;&gt;江苏省&lt;/option&gt;
|
JavaScript 前端开发
JavaScript函数柯里化的实现原理,进来教你完成一个自己的自动实现柯里化方法
JavaScript函数柯里化的实现原理,进来教你完成一个自己的自动实现柯里化方法
167 0
|
移动开发 JavaScript weex
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
weex-自定义module,实现weex在iOS的本地化,js之间互相跳转,交互,传值(iOS接入weex的最佳方式)
219 0
|
JavaScript
JS中实现或退出全屏
JS中实现或退出全屏
153 0