獲得焦點: onfocus
失去焦點: onblur
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style type="text/css">
input {
color: #999999;
}
</style>
</head>
<body>
<input type="text" name="" id="" value="文字" />
<script type="text/javascript">
//獲取元素
var text = document.querySelector('input');
//獲得焦點事件 onfocus
text.onfocus = function() {
if(this.value === '文字') {
this.value = ' ';
}
//獲得焦點把文本框內文字顏色變深
this.style.color = '#333';
}
text.onblur = function() {
if(this.value === ' ') {
this.value = '文字';
}
//失去焦點把文本框內文字顏色變淺
this.style.color = '#999';
}
</script>
</body>
</html>