<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div style="margin: 0 auto;width: 600px">
<input id="i1" type="text" onfocus="Focus()" onblur="Blur()" value="请输入关键字"/>
<input type="text" placeholder="请输入关键字">
</div> <script> function Focus() {
var tag = document.getElementById('i1');
var val =tag.value; // 获取text中的值
if(val=="请输入关键字"){
tag.value='';
}
} function Blur() {
var tag = document.getElementById('i1');
var val = tag.value;
if(val.length==0){//判断长度是不是0
tag.value="请输入关键字"
}
} </script> </body>
</html>
效果如下图: