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

看到朋友逍遥老鬼说到了这个地方,就顺带着写了一段给他。

作用是,一个文本框,需要输入内容,在没有输入的时候里面有一段提示内容,当点击这个文本框输入的时候,文本框内的内容自动消失。

View Code JAVASCRIPT
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
		window.onload = func;
	}else{
		window.onload = function(){
			oldonload();
			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);
<input id="test" type="text" value="请输入字符" />
加入书签:
  • Digg
  • del.icio.us
  • Sphinn
  • Facebook
  • Mixx
  • Google
  • YahooMyWeb
  • Yigg

原文链接|

目前 共有 6 条评论 ,点此发表评论

  1. 逍遥老鬼

    一月 6th, 2009 @ 08:39

    谢谢谢谢,代码收藏了。

    回复

  2. 张经纬

    一月 6th, 2009 @ 09:26

    别客气。

    回复

  3. 逍遥老鬼

    一月 12th, 2009 @ 08:50

    oldonload();
    这里写错了,有人在我博客说了,给你贴过来。

    回复

    张经纬 reply on 一月 12th, 2009 11:30:

    修改正确了。
    谢谢老鬼。

    回复

  4. pashuiyuer

    一月 13th, 2009 @ 02:43

    var oldonload = window.onload||{};
    window.onload = function(){
    oldonload();
    func();
    }

    回复

  5. 逍遥老鬼继续扯淡

    六月 20th, 2009 @ 09:08

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

      昨天,我发了一个input标签清空的日志,冰雪黑鹰说我的那个还不是很标准的,帮我重写了一个,现在的看着也简洁,而且也符合哪些什么标准,特别在这里感谢下。附上代码,以备后用……
    js部分:
    function addLoadEvent(func){
    var oldonload = window.onload;
    if (typeof window.onload != ‘function’){
    window.onload = func;
    }else{
    window.onload = f…