功能:点击用户消失,并且再次填入时颜色变化,如果里面什么都没有,失去焦点时会出现用户二字。
<!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>
|