input标签获取焦点时文本框内提示信息清空

逍遥老鬼 撰写  

  刚才荆棘鸟在群里讨论他的标签是否符合w3c的标准,贴上来一段代码,作用是,一个文本框,需要输入内容,在没有输入的时候里面有一段提示内容,当点击这个文本框输入的时候,文本框内的内容自动消失。挺简单的一个功能,但是没有想到实现起来还是很麻烦的,在网上找了一段代码,贴上来以备后用。

  好像html里面加上这样的js代码就不符合标准了……不过标准是死的,人是活得,不一定处处都符合标准。

  来源:闪吧

  解决思路:当需要填写的文本框很多时,在每个文本框内写上提示信息是个不错的办法,但这样一来用户在填写该项时必须先把提示文本删掉,非常不方便。所以应该让文本框更人性化一点,在focus时检查当前值是否为文本框的默认值,是则清空,否则保持不变,而在blur时检查文本框的内容是否为空,是则重置为默认值,否则保持不变。

  具体步骤:

  1.设置文本框的默认值或提示信息。

<input value="填写您的昵称">
<input value="填写您的生日(1900-01-01)">

  2.判断、清空文本框内容的函数。

function cls(){
with(event.srcElement)
if(value==defaultValue) value=""
}

  3.判断、还原文本框内容的函数。

function res(){
with(event.srcElement)
if(value=="") value=defaultValue
}

  4.给文本框添加触发事件onfocus和onblur,调用函数处理。完整代码:

<script>
function cls(){
//捕获触发事件的对象,并设置为以下语句的默认对象
with(event.srcElement)
//如果当前值为默认值,则清空
if(value==defaultValue) value=""
}
function res(){
//捕获触发事件的对象,并设置为以下语句的默认对象
with(event.srcElement)
//如果当前值为空,则重置为默认值
if(value=="") value=defaultValue
}
</script>
<input value="填写您的昵称" onfocus="cls()" onblur="res()">
<input value="填写您的生日(1900-01-01)"
onfocus="cls()" onblur="res()">

  注意:本例的方法对大部分表单控件都有效,比如多行文本框。

  特别提示:运行完整代码,在鼠标第一次单击文本框时该文本框内容将被清空,在文本框外单击时文本框内容将还原回默认值。如果改变了文本框的值,将不再有任何变化。


13 条评论

  1. 张经纬
    发表了 2009/01/05 在 1:05 上午 | 永久链接 | Reply

    function cls(){
    //清空函数
    }

    var inputId = document.getElementById(‘a’);

    inputId.onfocus = cls();

  2. 张经纬
    发表了 2009/01/05 在 1:14 上午 | 永久链接 | Reply

    无标题文档

    window.onload = test;
    function test(){
    var testid = document.getElementById(‘test’);

    function cls(){
    alert (‘ok’);
    }
    testid.onclick = cls;
    }

    给一个完整正确的。

  3. 发表了 2009/01/05 在 8:40 上午 | 永久链接 | Reply

    这个玩意怎么用啊?写详细点。

  4. 张经纬
    发表了 2009/01/05 在 9:28 上午 | 永久链接 | Reply

    其他的同你的是一样的。
    上面的代码是将JS和HTML分离出来。这样就符合分离的原则了。

    window.onload = test; //页面载入完毕后执行test函数

    function test(){
    var testid = document.getElementById(’test’); //取得ID为test的元素

    function cls(){ //定义的CLS函数(函数内容可以换成你需要的)
    alert (’ok’);
    }

    testid.onclick = cls; //test元素点击的时候,执行cls函数
    }

  5. 发表了 2009/01/05 在 9:57 上午 | 永久链接 | Reply

    给一段完整的代码看看,我对js一窍不通……

  6. 张经纬
    发表了 2009/01/05 在 10:18 下午 | 永久链接 | Reply

    好的。代码是这样的

    function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != 'function'){
    window.onload = func;
    }else{
    window.onload = function(){
    oladonload();
    func();
    }
    }
    }

    function checkText(){
    var textId = document.getElementById('test');
    var textDefault = '请输入字符';
    function cls(){
    if (this.value == textDefault){
    this.value = '';
    }
    }
    function res(){
    if (this.value == ''){
    this.value = textDefault;
    }
    }
    textId.onfocus = cls;
    textId.onblur = res;
    }
    addLoadEvent (checkText);

  7. 张经纬
    发表了 2009/01/05 在 10:18 下午 | 永久链接 | Reply

  8. 张经纬
    发表了 2009/01/05 在 10:19 下午 | 永久链接 | Reply

    HTML代码在回复内无法显示。我将双引号修改为全角的了。老鬼到时候将下面这段代码的双引号替换一下。

  9. 张经纬
    发表了 2009/01/05 在 10:20 下午 | 永久链接 | Reply

    好像还是不行哦。
    :(

  10. 张经纬
    发表了 2009/01/05 在 10:29 下午 | 永久链接 | Reply
  11. 发表了 2009/01/06 在 7:10 上午 | 永久链接 | Reply

    你可以直接发我qq上面……

  12. 发表了 2009/01/06 在 9:31 上午 | 永久链接 | Reply

    多谢老鬼,留个记号

  13. 发表了 2009/01/06 在 10:04 上午 | 永久链接 | Reply

    去看看第二篇关于这个的。

个引用通告

  1. [...] 看到朋友逍遥老鬼说到了这个地方,就顺带着写了一段给他。 [-]?View Code XHTML< !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"> <head> <meta http-equiv="Content-Type" content="text/html charset=utf-8" /> <title>无标题文档</title> <script type="text/javascript"> function addLoadEvent(func){ var oldonload = window.onload if (typeof window.onload != ‘function’){ window.onload = func }else{ window.onload = function(){ oladonload() func() } } }   function checkText(){ var textId = document.getElementById(‘test’) var textDefault = ‘请输入字符’ function cls(){ if (this.value == textDefault){ this.value = ” } } function res(){ if (this.value == ”){ this.value = textDefault } } textId.onfocus = cls textId.onblur = res } addLoadEvent (checkText) </script> </head>   <body> <input type="text" value="请输入字符" id="test" /> </body> </html> [...]

发表评论

Your email is never shared. 标记为 * 的为必填项目

*
*