试试看
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<h3>请直接点击 Check!注:IE6sp1, FF2 测试可用!</h3>
<input type="text" id="txtInput" value="123" /><input type="button" id="btnCheck" value="Check" />
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}$("btnCheck").onclick = function()
{
var oTxt = $("txtInput");
if (oTxt.value != "")
{
oTxt.value = ""; //清空text框,--这句好用
alert("谢谢输入!");
oTxt.focus();//重新将鼠标焦点放在text框内,结果好用
}
};
//-->
</script>
</body>
</html>
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<h3>请直接点击 Check!注:IE6sp1, FF2 测试可用!</h3>
<input type="text" id="txtInput" value="123" /><input type="button" id="btnCheck" value="Check" />
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}$("btnCheck").onclick = function()
{
var oTxt = $("txtInput");
if (oTxt.value != "")
{
oTxt.value = ""; //清空text框,--这句好用
alert("谢谢输入!");
oTxt.focus();//重新将鼠标焦点放在text框内,结果好用
}
};
//-->
</script>
</body>
</html>
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<h3>请直接点击 Check!注:IE6sp1, FF2 测试可用!</h3>
<input type="text" id="txtInput" value="123" /><input type="button" id="btnCheck" onclick="check(this)" value="Check" />
<script type="text/javascript">
<!--
function check()
{
var oTxt =document.getElementById("txtInput");
if (oTxt.value != "")
{
oTxt.value = ""; //清空text框,--这句好用
alert("谢谢输入!");
oTxt.focus();//重新将鼠标焦点放在text框内,结果好用
}
};
//-->
</script>
</body>
</html>$ 这个的用法 不太明白
我只是知道你 用这个定义了一个函数
还是不太明白你那样写有什么好处
是代码更通用
$函数是模仿prototype,简写。还可以这样写。
function $()
{
return document.getElementById(arguments[0]);
}
我也与到过这个问题,如果去掉alert("谢谢输入!"); 好像文本框就获得不了焦点 不清楚为什么
3L,我没试验你那个是不是能用,但是我们应该是先alert告诉用户信息输入的是错误的,然后再清空text框的内容的,你这个顺序反了
9L给的也不对?我那个其实开始就是加上了retrun false;没好用啊?代码基本是这样的:<td>
<input type="text" name="test" id = "test" value="" onchange="checkNumber(this, 5,2);" />
</td>在js方法checkNumber中:
……//先校验
if(errorStr!=""&&errorStr!=null){
alert(errorStr);
obj.value = "";
obj.focus();
return false;
}
不过感觉问题很可能出在checkNumber执行完毕以后!以下代码 IE6sp1, FF2 测试可用!
<!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>
<title> new document </title>
<meta name="generator" content="editplus" />
<meta name="author" content="Gao YiXiang" />
<meta name="email" content="[email protected]" />
<meta name="keywords" content="javascript dhtml dom" />
<meta name="description" content="I love web development." />
</head>
<body>
<h3>请直接点击 Check!注:IE6sp1, FF2 测试可用!</h3>
<input type="text" id="txtInput" value="123" /><input type="button" id="btnCheck" value="Check" />
<script type="text/javascript">
<!--
function $(sId)
{
return document.getElementById(sId);
}$("btnCheck").onclick = function()
{
var oTxt = $("txtInput");
if (oTxt.value != "")
{
alert("谢谢输入!请重新输入");
oTxt.value = ""; //清空text框,--这句好用
oTxt.focus();//重新将鼠标焦点放在text框内,结果好用
}
};
//-->
</script>
</body>
</html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>test</title>
<meta http-equiv="MSThemeCompatible" content="no" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<script language="javascript" >
function checktest(obj)
{
var objValue=obj.value;
if(objValue != ""){
alert(objValue);
obj.value = "";
obj.focus();
return false ;
}
}
</script>
</head>
<body>
<form action="" name="myform" method="">
<table width="100%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
测试
</td>
<td>
<input type="text" name="test" id = "test" value=""
onchange="checktest(this);" />
</td>
<td colspan="2" width="50%">
</td>
</tr>
</table>
</form>
</body>
</html>---还是清空text框的时候还好用,聚焦不好用
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title> test </title>
<meta http-equiv="MSThemeCompatible" content="no" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<script language="javascript" >
function checktest(obj)
{
var objValue=obj.value;
if(objValue != ""){
alert(objValue);
obj.value = "";
obj.focus();
// IE 下这样写吧!
event.returnValue = false;
//return false;
}}
</script>
</head>
<body>
<form action="" name="myform" method="post">
<table width="50%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
测试
</td>
<td>
<input type="text" name="test" id = "test" value="" onchange="checktest(this);" />
</td>
<td colspan="2" width="50%">
<input type="submit" />
</td>
</tr>
</table>
</form>
</body>
</html>
直接提交时,IE 下会先触发 onchange,从而阻止 submit 事件;
而 FF 下会直接提交,根本不触发 onchange!建议调整思路在 form 的 onsubmit 时统一进行表单检查!事件部分可以参考以下文章
http://www.cnblogs.com/RicCC/archive/2007/06/10/Cancel-Javascript-Event-in-IE7-and-Firefox.html
可是还不知道为什么那个return false会出这个问题呢?能给解释下么?
晚上结贴哈
而且 onchange 是在 onblur 之前被触发,换言之,onchange 时焦点并未离开,
至于 onchange 时 加入 return false 的奇怪反应,俺还真没搞明白,哈onchange EventThis event is fired when the contents are committed and not while the value is changing. For
example, on a text box, this event is not fired while the user is typing, but rather when the
user commits the change by leaving the text box that has focus. In addition, this event is
executed before the code specified by onblur when the control is also losing the focus. <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title> test </title>
<meta http-equiv="MSThemeCompatible" content="no" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="-1" />
<meta http-equiv="Cache-Control" content="no-cache" />
<script language="javascript" >
function checktest(obj)
{
var objValue=obj.value;
if(objValue != "")
{
alert(objValue);
obj.value = "";
obj.focus();
return false;
}
}
</script>
</head>
<body>
<form action="" name="myform" method="post">
<table width="50%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td>
测试
</td>
<td>
<input type="text" name="test" id = "test" value="" onblur="checktest(this);" />
</td>
<td colspan="2" width="50%">
</td>
</tr>
</table>
</form>
</body>
</html>
送你三本手册!DHTML参考手册
http://download.csdn.net/source/308913样式表中文手册
http://download.csdn.net/source/304124JScript语言参考
http://download.csdn.net/source/308916