js控制文本框光标聚焦到文字后面

代码:

<!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>
    <title></title>
</head>
<body>
    <input id="Text1" type="text" onfocus="moveEnd(this)" /><br />
     
    <input id="Button1" type="button" value="button" />
    <script type="text/javascript">
        function getArgs() {
            var args = new Object();
            var query = location.search.substring(1);
            query = query.replace(/\+/g, " ");
            var pairs = query.split("&"); // Break at ampersand
            for (var i = 0; i < pairs.length; i++) {
                var pos = pairs[i].indexOf(=); // Look for "name=value"
                if (pos == -1) continue; // If not found, skip
                var argname = pairs[i].substring(0, pos); // Extract the name
                var value = pairs[i].substring(pos + 1); // Extract the value
                value = decodeURIComponent(value); // Decode it, if needed
                args[argname] = value; // Store as a property
            }
            return args;
        }
        function Init() {
            var data = getArgs();
            if (data.keywords) {
                document.getElementById("Text1").value = data.keywords;
                document.getElementById("Text1").onfocus();
            }
        }
        Init();
        Button1.onclick = function () {
            var keywords = document.getElementById("Text1").value;
            var url = "HTMLPage5.htm?keywords=" + keywords;
            window.location.href = url;
        }

   function moveEnd(obj) { 
    obj.focus();
    var len = obj.value.length;
    if (document.selection) {
        var sel = obj.createTextRange();
        sel.moveStart(character, len);
        sel.collapse();
        sel.select();
    } else if (typeof obj.selectionStart == number && typeof obj.selectionEnd == number) {
        obj.selectionStart = obj.selectionEnd = len;
    }
}
    </script>
</body>
</html>

点击按钮刷新页面会发现焦点在文本框的内容的最后面

跟回车事件一起用比较好

js控制文本框光标聚焦到文字后面,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。