如何动态编写通用ie,fireFox设置焦点js

解决方案 »

  1.   


    <html>      
      
    <head>      
      
    <title>设置默认焦点</title>      
      
    <mce:script language ="javascript"><!--  
          
      
    function init(){      
      
    var ctrl=document.getElementById("UserName");      
      
    ctrl.focus();      
      
    }     
    // --></mce:script>      
      
    </head>      
      
        
      
    <body onload="init()" >      
      
    <h1>设置默认焦点</h1>      
      
    姓名:<input type="text" id="UserName">      
      
    </body>      
      
    </html> 
      

  2.   

    上面的有错,给你重来一个<!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=utf-8" />    
    <title>设置默认焦点</title>       
    <script language ="javascript">        
    function init(){        
    var ctrl=document.getElementById("UserName");       
    ctrl.focus();       
    }     
    </script>       
    </head>       
    <body onload="init()" >        
    <h1>设置默认焦点</h1>       
    姓名:<input type="text" id="UserName">       
    </body>
    </html>
      

  3.   

    不要意思没有描述清楚,我现在是这样的情况
    input id是动态的,在输入错误值onblur 之后再次获得焦点
      

  4.   

    那你肯定能得到该对象吧,调用focus就行了
      

  5.   

    ie调用对象focus()方法可以,firefox不行
      

  6.   

    因为在火狐中设置标签焦点的顺序是先失去焦点才能得到焦点,也就是说先document.getElementById('id').blur();之后再document.getElementById('id').focus();这样ID为id的标签就可以得到焦点啦