【问题标题】:Set focus to the end of input tag by javascript通过javascript将焦点设置到输入标签的末尾
【发布时间】:2011-03-23 09:18:12
【问题描述】:

敬礼。我的 javascript 有一些问题..

我制作了翻译脚本,允许格鲁吉亚语用户通过拉丁字母输入格鲁吉亚语。 所以,当我在输入标签上进行操作时,我无法将焦点设置到文本的末尾。正确地,光标位于文本的末尾,但如果文本比输入标签大得多,它必须自动滚动且不得隐藏。

这里是代码..

$(document).ready(function() {
    $(".geok").geokbd();
});
$.fn.geokbd = function() {
    symbols = "abgdevzTiklmnopJrstufqRySCcZwWxjh";
    this.relatedCheckbox = $("<input type=\"checkbox\" name=\"geokbd\" />").attr('checked', true);
    $(this).after(this.relatedCheckbox);
    $(this).keypress(
        function(e) {
            if ((e.charCode) == 96) {
                if ($(this).next('input').attr('checked')) {
                    $(this).next('input').attr('checked', false);
                } else {
                    $(this).next('input').attr('checked', true);
                }
                    return false;
                }
                if ($(this).next('input').attr('checked')) {
                    if ((index = symbols.indexOf(String.fromCharCode(e.charCode))) >= 0) {
                        ret = String.fromCharCode(index + 4304);
                            this.focus();
                            var scrollTop = this.scrollTop, 
                            start = this.selectionStart, 
                            end = this.selectionEnd;

                            var value = this.value.substring(0, start)  + ret + this.value.substring(end,this.value.length);

                            this.value = value;
                            this.scrollTop = scrollTop;
                            this.selectionStart = this.selectionEnd = start + ret.length;

                            return false;
                        }
                    }

                }
        );
}

谢谢

【问题讨论】:

  • 据我了解,原因是我手动为输入元素插入值,并且在按键时返回 false。如果我返回真,一切正常,但不必要的符号。

标签: javascript jquery html string input


【解决方案1】:

获取最后一个输入标签的id并用该id替换name,下面是基本逻辑

<script type="text/javascript">
function setFocus()
{
     document.getElementById("name").focus();
}
</script>
</head>

<body onload="setFocus()">
  <form>
       Name: <input type="text" id="name" size="30"><br />
       Surname: <input type="text" id="surname" size="30"> 
  </form>
</body>

【讨论】:

  • 阅读整个问题,而不仅仅是标题。
【解决方案2】:

你试过了吗:

function doSetCaretPosition (oField, iCaretPos) {
    // IE Support
    if (document.selection) {
        // Set focus on the element
        oField.focus ();
        // Create empty selection range
        var oSel = document.selection.createRange();
        // Move selection start and end to 0 position
        oSel.moveStart ('character', -oField.value.length);
        // Move selection start and end to desired position
        oSel.moveStart ('character', iCaretPos);
        oSel.moveEnd ('character', 0);
        oSel.select ();
    }
    // Firefox support
    else if (oField.selectionStart || oField.selectionStart == '0') {
        oField.selectionStart = iCaretPos;
        oField.selectionEnd = iCaretPos;
        oField.focus ();
    }
}
$(document).ready(function() {
    $("#yourElement").focus();
    doSetCaretPosition(document.getElementById("yourElement"),999);
});  

(来源:http://www.webdeveloper.com/forum/archive/index.php/t-74982.html

【讨论】:

  • 我不想将焦点放在 document.ready 上。我希望它在按键上。所以,它不起作用
【解决方案3】:

有几个很好的答案

Use JavaScript to place cursor at end of text in text input element

例如,你可以使用

        <input id="search" type="text" value="mycurrtext" size="30" 
        onfocus="this.value = this.value;" name="search"/>

或者您可以使用 JQuery 插件:PutCursorAtEnd。作者甚至在那里发布了源代码,我测试了它是否适用于 Opera。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多