文本框特效:获取焦点时自动清除默认文本 失去焦点重新显示默认文字

代码

<input style="background-image:url(input.gif);background-repeat:no-repeat;padding-left:46px;" type='text' name='keyword' size='23' value='输入关键字' maxlength='50' onfocus="if(this.value='输入关键字')this.value=''" onblur="this.value='输入关键字'"/>

background-image:url(input.gif); 给文本框设置背景图片

onfocus="if(this.value='输入关键字 ')this.value=''" 获取焦点(即鼠标点击文本框)时自动清除文本框文字内容

onblur="this.value='输入关键字'"/ 失去焦点(即鼠标点击文本框以外的区域)时返回显示文本框默认文字内容

只清除文本时还可以用 onClick=this.value=''"

 

----------------------------------------------------------------------------------------------------

输入框失去焦点时 默认值为灰色  代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>烈火网 - 关注站长,建设互联网!</title>
<style>
html,body{margin:200px 400px;}
#search{color:#aaa;270px;border:1px solid #CC0000;padding:1px; }
</style>
</head>
<body>
<input type="text" value="烈火网(LieHuo.Net)-关注站长,建设互联网!" id="search" />
<script language="javascript">
    var s=document.getElementById("search");
    s.onfocus=function(){if(this.value==this.defaultValue)this.value=''};
    s.onblur=function (){if(/^\s*$/.test(this.value)){this.value=this.defaultValue;this.style.color='#aaa'}}
    s.onkeydown=function(){    this.style.color='#333'}
</script>
</body>
</html>

原文地址:https://www.cnblogs.com/buffer/p/1711335.html