试试看
<!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>

解决方案 »

  1.   

    为什么不这么写啊
    <!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>$ 这个的用法 不太明白
    我只是知道你 用这个定义了一个函数
    还是不太明白你那样写有什么好处
    是代码更通用
      

  2.   

    $(sId)纯属个人习惯,只是代码短了一些而已,哈
      

  3.   

    代码没有问题。点击check按钮以后清空input然后焦点定位在input中。
    $函数是模仿prototype,简写。还可以这样写。
    function $()
    {
        return document.getElementById(arguments[0]);
    }
      

  4.   

    没什么特别的  就是是代码短了  看着简洁了
    我也与到过这个问题,如果去掉alert("谢谢输入!"); 好像文本框就获得不了焦点  不清楚为什么
      

  5.   

    最后要加上return false;不然刚给对象设了焦点,焦点又跑到你点击的那个按钮上面了
      

  6.   

    嘿嘿周末家里上不了网,没来看回复
    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;
    }
      

  7.   

    执行顺序调整一下就可以了,至于lz的代码逻辑,由于只有一些片断,因此无法调试,
    不过感觉问题很可能出在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>
      

  8.   

    恩,你的好用,但是没看明白为啥我这个测试代码就是不好用。。<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框的时候还好用,聚焦不好用
      

  9.   

    应该是 return false; 的毛病!改了改,现在适用 IE,需要兼容 FF 么?
    <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> 
      

  10.   

    其实俺对 FF 了解甚少,刚才测了一下 FF,发现事件顺序居然与 IE 都不一样,服了!
    直接提交时,IE 下会先触发 onchange,从而阻止 submit 事件;
    而 FF 下会直接提交,根本不触发 onchange!建议调整思路在 form 的 onsubmit 时统一进行表单检查!事件部分可以参考以下文章
    http://www.cnblogs.com/RicCC/archive/2007/06/10/Cancel-Javascript-Event-in-IE7-and-Firefox.html
      

  11.   

    哎,那先就这样吧,
    可是还不知道为什么那个return false会出这个问题呢?能给解释下么?
    晚上结贴哈
      

  12.   

    你可以在onsubmit上来验证input,也可以自己模拟onsubmit,当点击按钮时来触发验证函数,在onchange事件里面写focus()会有问题。
      

  13.   

    在FF下,一般情况下submit 事件优先onChange 当域失去焦点且域值相对于onFocus执行有所改变时执行.
      

  14.   

    又去测了一下,发现问题出在 onchange 上,改用 onblur 就没问题了,
    而且 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