<body>
<input type="text" value="你没有点我">
<script>
// 1.获取元素
var text = document.querySelector('input');
text.style.color = '#999';
// 2.注册事件,获取焦点事件
text.onfocus = function(){
if(text.value === '你没有点我'){
text.value = ''; //让输入框的文字消失
this.style.color = '#000';
}
}
// 3.注册事件,失去焦点事件
text.onblur =function(){
if(text.value === ''){
text.value = '你没有点我'; //让输入框的文字显示
text.style.color = '#999';
}
}
</script>
</body>
10-22
686
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)