JS muestra "entrada" "error"

Función: haga clic en el usuario para desaparecer, y el color cambia cuando se vuelve a llenar.Si no hay nada en él, aparecerá la palabra "Usuario" cuando se pierda el foco.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title></title>
	<style type="text/css">
		input {
			color: #999;
			outline: none;
		}
	</style>
</head>
<body>
    <div>
        <input type="text" value="用户" id="input1">
        <input type="text" value="明文密码" id="input2">
    </div>
	<script type="text/javascript">
		// 1.获取元素 
		var ipt1 = document.getElementById('input1');
		var ipt2 = document.getElementById('input2');
		// 2.操作元素
		ipt1.onfocus = function(){
			if(this.value==='用户'){
				this.value='';
			}
			this.style.color = '#333';
		}
		ipt1.onblur = function(){
			if(this.value===''){
				this.value='用户';
			}
			this.style.color = '#999'
		}
		ipt2.onfocus = function(){
			if(this.value==='明文密码'){
				this.value='';
			}
			this.style.color = '#333';
		}
		ipt2.onblur = function(){
			if(this.value===''){
				this.value='明文密码';
			}
			this.style.color = '#999'
		}
	</script>
</body>
</html>

Supongo que te gusta

Origin blog.csdn.net/weixin_60789461/article/details/123610985
Recomendado
Clasificación