bootstrap, 怎么切换input的类型,从text换成password?

如图所示,点一个右边的字体图标,就把左边的input的类型从text换成password?
怎么做,我看有些网站就是类似这样的,点一下,就显示密码,再点一下,就不显示了,变成了小黑点。

https://img1.mukewang.com/5b9ccd6300019c5e03760255.jpg


代码:

<div class="form-group">
    <div class="col-md-12">
        <div class="input-group">
            <span class="input-group-addon" id="basic-addon1">密&nbsp;&nbsp;&nbsp;&nbsp;码</span>
            <input id="pass_word" name="pass_word" type="text" class="form-control" placeholder="请输入密码" aria-describedby="basic-addon1">                            
            <span class="input-group-addon"><i class="glyphicon glyphicon-eye-open"></i></span>
        </div>
    </div>
</div>


翻翻过去那场雪
浏览 996回答 1
1回答

隔江千里

&nbsp; &nbsp; <input type="text" id="psw">&nbsp; &nbsp; <button id='btn'>按钮</button>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; var obj = document.getElementById('btn');&nbsp; &nbsp; &nbsp; &nbsp; var psw = document.getElementById('psw');&nbsp; &nbsp; &nbsp; &nbsp; obj.onclick = function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(psw.getAttribute('type') == 'text'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; psw.setAttribute('type','password')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; psw.setAttribute('type','text')&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript