<html>
    <head>
    <script type="text/javascript">
function aa(){
if(document.getElementById('txt1').value == ''){
alert('please input some text');
}
}
    </script>
    </head>
<body>
<form>
    <input id="txt1" type="text" value="" onblur="aa();" />
    <input id="txt2" type="text" value="" />
    <input id="txt3" type="text" value="" />
<input id="submit" type="submit" />
</form>
</body>
    </html>如果txt1中为空时光标转移的话会触发onblur 此时alert了   那么在按下确定后让光标回到txt1呢

解决方案 »

  1.   

        if(document.getElementById('txt1').value == ''){
         alert('please input some text');
         document.getElementById('txt1').focus();
        }
      

  2.   


    ie下可以,但是ff下就不行了 
      

  3.   


        <html>
        <head>
        <script type="text/javascript">
    function aa(obj){
    if(obj.value == 'a'){
    alert('please input some text');
    obj.focus(); 
    }
    }
        </script>
        </head>
    <body>
    <form>
        <input id="txt1" type="text" value="" onblur="aa(this);" />
        <input id="txt2" type="text" value="" onblur="aa(this);" />
        <input id="txt3" type="text" value="" onblur="aa(this);" />
    <input id="submit" type="submit" />
    </form> </body>
        </html>改成通用点的了,但是在ff下还是不行,我的ff是3.5.2的   奇怪中 
      

  4.   

    window.setTimeout(function(){ obj.focus(); }, 0);
    google到   都是要加上这句话,不过为什么还是没明白
    JS水平太菜了  -_-!
      

  5.   

    if(document.getElementById('txt1').value == ''){ 
        document.getElementById('txt1').focus(); 
        alert('please input some text'); 
        } 换个顺序试试
      

  6.   

    <input id="txt1" type="text" value="" onblur="aa();" />
    你这个调用时候多了一个“;”吧。
      

  7.   

    的确是有问题,
    刚才我在IE和ff下也试验过了,ff下用window.setTimeout(function(){ obj.focus(); }, 0); 
    这个也没用。
      

  8.   

    不过如果没填写就一直给这个提示会很烦的。
    关闭页面的时候也要谈出alert的内容,切换窗口也要谈出来。
    超级不爽,
      

  9.   

    没有找到方法。
    ff下始终不成功。
    不过alert不是友好的方式
    最好用层来显示错误信息
      

  10.   

    试试
    document.getElementById('txt1').select();
      

  11.   

      <html>
        <head>
        <script type="text/javascript">
        function aa(){
        if(document.getElementById('txt1').value == ''){
        alert('please input some text');
    document.getElementById('txt1').focus();
    document.getElementById('txt1').select();
        }
        }
        </script>
        </head>
        <body>
        <form>
        <input id="txt1" type="text" value="" onblur="aa();" />
        <input id="txt2" type="text" value="" />
        <input id="txt3" type="text" value="" />
        <input id="submit" type="submit" />
        </form>
        </body>
        </html>
    都用上试一试
      

  12.   

    在ff下window.setTimeout(function(){ obj.focus(); }, 0);  我这边是可用的
    多谢各位的帮助,可能结贴有点早了,呵呵,下次慢点结贴就是了  呵呵
      

  13.   

     if(document.getElementById('txt1').value == ''){ 
        alert('please input some text'); 
        document.getElementById('txt1').focus(); 
        }//focus();得到焦点
    //onblur();失去焦点
      

  14.   

    alert之后  加一个document.getElementById('txt1').focus(); 就行了啊