输入框密码切换显示或隐藏功能的实现

  在一些网站或web app登陆的时候输入密码时有一个显示或者隐藏密码字符的开关,个人想到实现方法应当是js动态修改input标签的type属性,当type为text的时候会显示密码内容,而type为password的时候则会显示为圆点或者星号等,以下是demo:

  html:

1 <input type="password" id="pwd">
2 <button id="clc">click</button>

  js:

    var pwd = document.querySelector('#pwd');
    document.querySelector('#clc').addEventListener('click',function(){showPwd(pwd)},false);
    function showPwd(obj){
        var type = obj.getAttribute('type');
        if(type=='password'){
            obj.setAttribute('type','text');
        }else{
            obj.setAttribute('type','password');
        }
    }
原文地址:https://www.cnblogs.com/ihaveahammer/p/4356001.html