HTML5表单中password输入框的文字显示与隐藏实现

标签:
it |
分类: html |
@问题描述与思路
HTML5表单中对于密码输入框password类型可以隐藏用户输入的内容,但有时候会用到允许用户自由显示或者隐藏输入框内容:要实现这个功能首先想到的是用js动态改变input的type类型,觉得将type
放两个input,一个是password,一个是text,共同监听用户的输入事件,用户每次切换我们用js控制两个input的显示与隐藏来实现此效果。
实现步骤:
1.首先写好HTML界面标签以及css样式(其中的text的input开始先隐藏:style="display:none",后面显示和隐藏操作也通过改变display的属性来实现):
body{ margin:0px; background-image:url('background.png'); font-family: 'PT Sans', Helvetica, Arial, sans-serif; text-align: center; color: #A6A6A6; } #page-container{ margin-top:200px; } input{ width: 100%; height: 50px; border:none; padding-left:3px; font-size: 18px; } #username{ border-radius: 7px 7px 0px 0px; border-bottom:1px solid #DCDCDC; } .password{ border-radius: 0px 0px 7px 7px; } input:focus { outline: none; } img{ position: absolute; right: 0px; margin: 15px; } button{ width: 200px; height: 50px; margin-top: 25px; background: #1E90FF; border-radius: 10px; border:none; font-size: 18px; font-weight: 700; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,.1); } button:hover { background: #79A84B; outline: 0; }
2.然后要用JS实现点击事件的交替操作:开始密码是隐藏的,点击后面的小眼睛图标显示密码,也就是把password的input隐藏然后把text的input显示出来,同时注意要把password的值传到text里面去,反过来一个道理:
var visible=document.getElementByIdx_x_xx_x_x_x('psw-visible');//text var invisible=document.getElementByIdx_x_xx_x_x_x('psw-invisible');//password //隐藏text,显示password function showPsw(){ var val=$('#hide').val();//将password的值传给text $('#show').val(val); invisible.style.display = "none"; visible.style.display = ""; } //隐藏password,显示text function hidePsw(){ var val=$('#show').val();//将text的值传给password $('#hide').val(val); invisible.style.display = ""; visible.style.display = "none"; }