感觉挺简单的,直接上代码吧,看完应该就理解了。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="">
账号:<input type="text" id="username" /><br/>
密码:<input type="password" id="password" /><br/>
<label onchange="show_pwd()">
<input type="checkbox" id="" />显示密码
</label>
</form>
<script type="text/javascript">
var pwd = document.getElementById("password");
function show_pwd(){
if(pwd.type == "password"){
pwd.type = "text"; // 改变input的属即可实现
}else {
pwd.type = "password";
}
}
</script>
</body>
</html>
补充:
1.设置 label 标签是为了点击文字时,也会勾选CheckBox。
2.label 里面的事件还可以用onclick事件。
说明:这里只是介绍了一种方法,可能有多种方法可以实现该功能,可以自行发掘。
2019年9月26日更新: