【代码笔记】HTML+CSS+JavaScript实现密码输入框显示文字

原理:

通过设置一个input为password,默认display为none,再设置一个input为text设置为block,利用js脚本控制显隐。

具体思路:

给type类型为text的输入框加一个点击事件,点击事件是设置text类型输入框的display为none,设置password类型输入框为block,并获取焦点。

给type类型为password的输入框加一个失去焦点事件,失去焦点事件是先判断password是否为空。如果为空,就设置text类型输入框的display为block,设置password类型输入框为none;如果不为空,就结束方法。

具体实现代码:

JS文件:

function changePwd(obj) {
	obj.style.display = "none";
	if (obj.type == "text") {
		document.getElementById('pwdtext').style.display = "block";
		document.getElementById('pwdtext').focus();
	} else {
		if (obj.value == "")
			document.getElementById('pwdshow').style.display = "block";
		else {
			obj.style.display = "block";
		}
	}
}

HTML/JSP文件:

<div class="pwd">
	<input type="password" name="pwd" id="pwdtext" οnblur="changePwd(this);" />
<span style="white-space:pre">	</span><input type="text" id="pwdshow"  value="Password" οnfοcus="changePwd(this);" />
</div>

CSS文件:

.pwd {
    margin-top: 20px;
    margin-left: 40px;
    border: 1px solid #dcdcdc;
    width: 348px;
    height: 40px;
    line-height: 40px;
}
.pwd input {
    color: #dcdcdc;
    width: 289px;
    border: 0;
    line-height: 40px;
    float: left;
}
#pwdtext {
    outline: none;
color: #000000;
display: none;
}

效果图:



  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值