密碼的顯示與隱藏(js)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			.box {
				width: 400px;
				border-bottom: 1px solid #ccc;
				margin: 100px auto;
				position: relative;
			}
			.box input {
				width: 370px;
				height: 30px;
				border: 0;
				outline: none;
			}
			.box img {
				position: absolute;
				top: 2px;
				right: 2px;
				width: 24px;
			}
		</style>
	</head>
	<body>
		<div class="box">
			<label><img src="../apiimg/閉眼.png" id="eye" alt=""></label>
			<input type="password" id="pwd" name="" />
		</div>
		<script type="text/javascript">
			//獲取元素
			var eye = document.getElementById('eye');
			var pwd = document.getElementById('pwd');
			//註冊事件 程序處理
			var flag = 0;
			eye.onclick = function() {
				// 點擊一下flag變化
				if (flag == 0) {
					pwd.type = 'text';
					eye.src = '../apiimg/眼睛.png';
					flag = 1;  //賦值
				} else {
					pwd.type = 'password';
					eye.src = '../apiimg/閉眼.png';
					flag = 0;
				}
			}
			
		</script>
		
	</body>
</html>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章