html密码框不显示,密码框中密码的显示与隐藏切换(JS)

目标:

点击小眼睛后,

1、密码框变成文本框;

2、小眼睛图片由闭眼变成睁眼;

3、再次点击后,又变成闭眼+密码框

要想实现3,方法1:

则需要一个变量,来辅助判断当前input的属性,如果flag为0,则在点击后,将input的type属性改为text框,同时,再将flag值重新赋为1.此时,再次点击,又会进行判断,而这次的flag=1,所以,就直接将input的type属性改为了password

要想实现3,方法2:

直接对input的type属性进行判断,若为text,则改为password,若为password,则改为text。

代码如下:

Document

/*设置父盒子的样式*/

div {

position: relative;

width: 400px;

border-bottom: 2px solid #ccc;

margin: 100px auto;

}

/*设置密码框的样式*/

input {

width: 370px;

height: 30px;

border: none;

outline: none;

}

/*利用定位,设置小眼睛图片样式*/

img {

position: absolute;

top: 5px;

right: 1px;

width: 24px;

}

//步骤:

//1、获取元素

var ipt = document.querySelector('input');

var img = document.querySelector('img');

//2、注册事件

//方法1:

// var flag = 0;

// img.onclick = function() {

// if (flag == 0) {

// ipt.type = 'text';

// img.src = './images/open.png';

// flag = 1;

// } else {

// ipt.type = 'password';

// img.src = './images/close.png';

// flag = 0;

// }

// }

//方法2:

img.onclick = function() {

if (ipt.type === 'password') {

ipt.type = 'text';

img.src = './images/open.png';

} else {

ipt.type = 'password';

img.src = './images/close.png';

}

}

图片是找的京东上的眼睛睁闭图,如果想要测试,可以不用img图片,给 i 标签设置宽高并定位,给一个背景色,写JS时,可以将 i 标签设置成事件源,通过点击 i 标签的范围,来切换input的type属性。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用以下代码来实现Bootstrap密码显示隐藏功能: HTML代码: ```html <div class="form-group"> <label class="col-xs-2 control-label">原始密码:</label> <div class="col-xs-10"> <div class="block input-icon input-icon-right"> <input name="oldPassword" id="oldPassword" type="password" class="form-control" placeholder="请输入原始密码"> <i class="ace-icon fa fa-eye" title="显示密码" id="see_oldpwd" onclick="seePwd(this)"></i> </div> </div> </div> ``` JavaScript代码: ```javascript //密码隐藏显示 function seePwd(obj){ var pwdValue = $(obj).parent().children('input:first-child').val(); //获取密码值 var _type = $(obj).parent().children('input:first-child').attr('type'); //获取input的type类型 if(_type == 'password'){ $(obj).parent().children('input:first-child').attr('type','text'); $(obj).attr('class','ace-icon fa fa-eye-slash'); $(obj).attr('title','隐藏密码'); }else if(_type == 'text'){ $(obj).parent().children('input:first-child').attr('type','password'); $(obj).attr('class','ace-icon fa fa-eye'); $(obj).attr('title','显示密码'); } $(obj).parent().children('input:first-child').val(pwdValue); } ``` 这段代码使用了Bootstrap的样式和JavaScript函数来实现密码显示隐藏。当用户点击密码后面的眼睛图标时,JavaScript函数`seePwd`会被触发。该函数会获取密码的值和类型,并根据类型来切换密码显示状态。如果密码的类型为`password`,则将其类型改为`text`,从而显示密码;如果密码的类型为`text`,则将其类型改回`password`,从而隐藏密码。同时,函数还会更新眼睛图标的样式和标题,以便提示用户当前密码的状态。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Bootstrap用户输入密码显示隐藏代码](https://download.csdn.net/download/weixin_38683195/12515022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [bootstrap显示隐藏密码](https://blog.csdn.net/qq_36134977/article/details/103991162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值