<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>文档</title>
<script language="JavaScript" type="text/javascript">
function test(obj){
alert(obj.value)
obj.focus();
}
</script>
</head>
<body >
<table id="table1" width="200" border="1">
  <tr>
  <td><input type=text id="txt1" onchange="test(this)" /></td>
  <td>aa</td>
  </tr>
  <tr>
  <td>b</td>
  <td><select id="selid"><option value=1>sel1</option><option value=2>sel2</option></select></td>
  </tr>
</table>
</body>
</html>
为什么我在input框输入文字后,鼠标直接点击select标签的话,
触发完JS函数后,焦点是在select标签上的,
而不是在input上

解决方案 »

  1.   

    因为你在input输入文字移动焦点到select标签时js就执行完了,再点击select标签是不会触发js的,当然就不行了
      

  2.   

    我代码是先alert再给焦点的啊,
    不是说alert 能把程序停下来吗?
      

  3.   


    <input type=text id="txt1" onchange="test(this)" />
    这个只有在input里面的值被修改了后才触发事件的
    你可以换成blur
    <input type=text id="txt1" onblur="test(this)" />
      

  4.   

    to 1楼:
    js那时候还没结束,是点了确定后才运行focus的,照你这么说,为什么我点其他的控件,比如textbox是正确的
    to 3楼:
    所有触发类型都一样  都会定位到select的  
      

  5.   

    to 6楼:
       onblur也一样,只要鼠标移动到select上就这样
    不信 你就建个txt文本,复制一下楼主的代码  自己用onblur测试一下
      

  6.   


    <input type=text id="txt1" onblur="test(this)" />楼主在input中一般用onblur这个事件,当是select的时候用onchange
      

  7.   

    to 9楼  onblur也是一样的
      

  8.   

    其实不管你是输入完之后点select还是其他地方,焦点都是没有回去的.在执行一段脚本时,对dom的操作不是即时生效的。浏览器可能执行完当前脚本所有代码后才真正处理脚本中对dom的操作。因此我在处理这种情况的时候,在settimeout指定的函数中执行某个input的focus()方法 
    <!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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>文档</title>
    <script language="JavaScript" type="text/javascript">
    function test(obj){
    alert(obj.value)
    //obj.focus();
    window.setTimeout("document.getElementById('txt1').focus();", 50)  ;
    }
    </script>
    </head>
    <body >
    <table id="table1" width="200" border="1">
      <tr>
      <td><input type=text id="txt1" onchange="test(this)" /></td>
      <td>aa</td>
      </tr>
      <tr>
      <td>b</td>
      <td><select id="selid"><option value=1>sel1</option><option value=2>sel2</option></select></td>
      </tr>
    </table>
    </body>
    </html>
    参考资料
      

  9.   

    参考资料的那个帖子说FF与IE不同,但是楼主的代码我在FF和Chrome中试过,仍然不能如意而使用window.setTimeout("document.getElementById('txt1').focus();", 50)  ;
    3个浏览器都是一样的,即在select获取焦点后,焦点马上闪回'txt1'
      

  10.   

    to 13
    点击其他textbox(即input),或者点击空白处是可以把通过obj.focus回去的,只是点击了dropdownlist(即select)焦点回不去
      

  11.   

    你应该想想当你鼠标直接点击select标签的话,就会触发了select的focus()事件
    焦点当然会在select上,不在的话才会奇怪呢
      

  12.   


    不是这个的,我测试过了如果你点到input text也是不行的他也会把焦点放在你点击的这个
    text上 
      

  13.   

    onchange只有在input里面的值被修改了后才触发事件的,可以换成blur
      

  14.   

    onchange只有在input里面的值被修改了后才触发事件的,你可以换成onblur