JavaScript实现搜索框效果

要求:搜索框获取焦点的时候如果里面的内容是“请输入关键字”,则清空,否则不清空
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>搜索文本框效果</title>
	<script type="text/javascript" src="js/jquery-1.8.3.js"></script>
	<script type="text/javascript">
	//页面加载就获取焦点
	function Focus(){
		document.getElementById('text1').focus();
	}
	$(function(){
		$("#text1").focus(function(){
			$(this).select();
			$(this).css("background-color","orange");
			if(this.value=="请输入关键字!"){
					this.value="";
				}
		})
		$("#text1").blur(function(){
			$(this).css("background-color","white");
			if(this.value==""){

				this.value="请输入关键字!";
				}
		})
	})
	</script>
</head>
<body οnlοad="Focus();">
	搜索:<input type="text" id="text1" value="请输入关键字!">
</body>
</html>

原文地址:https://www.cnblogs.com/a1111/p/12816669.html