如我的TextBox已经有文字"你好",焦点就要在"好"的后面,就是在最后一个文字的后面我用 document.getElementById("TextBox1").focus(); ,焦点在你的前面

解决方案 »

  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>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
    <title>无标题文档</title>
    <script language="javascript" type="text/javascript">
    function toInput()
    {
    document.forms["myForm"].elements["input1"].focus();
    document.forms["myForm"].elements["input1"].value=document.forms["myForm"].elements["input1"].value;
    }
    window.onload=new Function("document.forms[\"myForm\"].elements[\"input1\"].style.width=\"500px\";");
    </script>
    </head>
    <body>
    <form name="myForm">
    <div><input type="text" name="input1" value="为什么按确定光标停在左边,我想在右边^-^" /></div> 
    <input type="button" onclick="toInput()" value="确定" />
    </form>
    </body>
    </html>[/code]
      

  2.   

    来自:http://blog.csdn.net/liang4571231/archive/2009/04/20/4093522.aspx<script language="javascript" type="text/javascript">
           
                document.getElementById("txtName").focus();
                var esrc = document.getElementById("txtName");
                if (esrc == null) {
                    esrc = event.srcElement;
                }
                var rtextRange = esrc.createTextRange();
                rtextRange.moveStart('character', esrc.value.length);
                rtextRange.collapse(true);
                rtextRange.select();
                
    </script>