focus方法 使得元素得到焦点并执行由 onfocus 事件指定的代码。

解决方案 »

  1.   

    关键是我点击的是一个链接:
    <a href="/site/ViewResource.do?accountId=<bean:write name="resourceBean" property="accountId"/>&resourceId=<bean:write name="resourceBean" property="id"/>#add">add comment</a>
    怎么能够同时执行focus方法?
      

  2.   

    <a onclick="test('/site/ViewResource.do?accountId=<bean:write name="resourceBean" property="accountId"/>&resourceId=<bean:write name="resourceBean" property="id"');">
    function test(url) {
        location.href=url;
        obj.focus();
    }
      

  3.   

    我现在修改如下:
    <SCRIPT LANGUAGE="JavaScript">
    function test() {
        //location.href=url;
        var obj2=document.getElementsByName("comments");
        if (obj2[0].type=="textarea")
        {  
           obj2[0].focus()
        }
    }
    </SCRIPT>
    <a onclick="test()" href="/site/ViewResource.do?accountId=<bean:write name="resourceBean" property="accountId"/>&resourceId=<bean:write name="resourceBean" property="id"/>#add">
    想把焦点定位到comments内,但现在锚点可以正确定位,但焦点没有定位到comments内,请问那里不对呢?
      

  4.   

    <SCRIPT LANGUAGE="JavaScript">
    function test(url) {
        window.location.href=url;
        var obj2=document.getElementsByName("comments");
        if (obj2[0].type=="textarea")
        {  
           obj2[0].focus()
        }}
    </SCRIPT>
    <a onclick="test('/site/ViewResource.do?accountId=<bean:write name="resourceBean" property="accountId"/>&resourceId=<bean:write name="resourceBean" property="id"/>#add');">add comment</a>
    第一次不能定位焦点,第二次以后都可以,怎么回事呢?