题目:显示隐藏文本框内容
作用:当鼠标点击文本框时,里面的默认文字隐藏;当鼠标离开文本框时,里面的文字显示。
分析:
- 首先表单需要两个新事件,获取焦点onfocus,失去焦点onblur
效果:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>显示隐藏文本框内容</title>
<style>
input {
color: #999;
}
</style>
</head>
<body>
<input type="text" value="手机">
<script>
var text = document.querySelector('input');
text.onfocus = function() {
// console.log("得到焦点");
if(text.value == '手机') this.value = '';
this.style.color = '#333';
}
text.onblur = function() {
// console.log("失去焦点");
if(text.value == '') this.value = '手机';
this.style.color = '#999';
}
</script>
</body>
</html>