input标签获取焦点时文本框内提示信息清空(2)
2009年1月6日
9 条评论
昨天,我发了一个input标签清空的日志,冰雪黑鹰说我的那个还不是很标准的,帮我重写了一个,现在的看着也简洁,而且也符合哪些什么标准,特别在这里感谢下。附上代码,以备后用……
js部分:
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);
html部分:
<input type="text" value="请输入字符" id="test" />