JS 输入框为空的使用
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<script>
onload = function () {
var txtObj = document.getElementById('txt');
txtObj.onfocus = function () {
if (this.value == '请输入搜索关键词' && this.style.color=='gray') {
this.value = '';
this.style.color = 'black';
}
}
txtObj.onblur = function () {
if (this.value == '') {
this.style.color = 'gray';
this.value = '请输入搜索关键词';
}
}
}
</script>
</head>
<body>
<input type="text" value="请输入搜索关键词" style="color:gray" id="txt"/>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<meta charset="utf-8" />
<script>
onload = function () {
var txtObj = document.getElementById('txt');
txtObj.onfocus = function () {
if (this.value == '请输入搜索关键词' && this.style.color=='gray') {
this.value = '';
this.style.color = 'black';
}
}
txtObj.onblur = function () {
if (this.value == '') {
this.style.color = 'gray';
this.value = '请输入搜索关键词';
}
}
}
</script>
</head>
<body>
<input type="text" value="请输入搜索关键词" style="color:gray" id="txt"/>
</body>
</html>