<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<style>
input {
color: #999;
}
</style>
</head>
<body>
<input type="text" value="手机">
<script>
// 1.获取元素
var text = document.querySelector('input');
// 2.注册事件 获得焦点事件 onfocus
// 获得焦点需要把文本框里面的文字颜色变黑
text.onfocus = function() {
if (this.value === '手机') {
this.value = '' //获取焦点让里面内容变空
}
this.style.color = '#333' //更改颜色
}
// 3. 注册事件 失去焦点事件 onblur
// 失去焦点需要把文本框里面的文字颜色变浅色
text.onblur = function() {
if (this.value == '') {
this.value = '手机'; //更改文本内容
}
this.style.color = '#999' //更改颜色
}
</script>
</body>
</html>