onfocus得到焦点触发事件
onblur失去焦点时触发事件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
input {
color: #999999;
}
</style>
</head>
<body>
<strong>onfocus,onblur</strong>
<input type="text" value="手机">
<script>
var text = document.querySelector('input');
text.onfocus = function () {
// console.log('得到焦点');
if (this.value == '手机') {
this.value = '';
}
this.style.color="#333";
}
text.onblur = function () {
// console.log('失去焦点');
if (this.value == '')
this.value = '手机';
this.style.color='#999'
}
</script>
</body>
</html>