js搜索框焦点获取与失去怎么实现?

发布时间:2019-10-08编辑:脚本学堂
javascript实现搜索框点击文字消失失焦时文本出现的效果,文本框焦点获取与消失,用js脚本实现很简单,需要的朋友参考下。

例子,js控制文本框焦点。
当获焦时,文本消失,失焦时文本出现。

代码:
 

复制代码 代码示例:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js搜索框焦点显示与消失_www.jb200.com</title>
</head>
<body>
<input id="text" type="text" value="点我就消失"/>
<script>
var oText=document.getElementById("text");
var onoff=true;
oText.color="#000";
oText.onfocus=function(){
if(onoff){
this.value="";
this.color="red";
onoff=false;
}
}
oText.onblur=function(){
if(this.value==''){
this.color="#000";
this.value="点我就消失";
onoff=true;
}
}
</script>
</body>
</html>