通过javascript将焦点设置到输入标记的末尾

发布于 2024-10-26 09:33:45 字数 1758 浏览 1 评论 0原文

敬礼。我的 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;
                        }
                    }

                }
        );
}

谢谢

Salut. i have some problem with javascript..

I made translate script, that allows Georgian users type Georgian by Latin alphabet.
So, when i manipulate on input tag, i can't set focus to the end of the text.. correctly, cursor is in the end of text but if text is much bigger than input tag, it must scroll automatically and must not hide.

here's code..

$(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;
                        }
                    }

                }
        );
}

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深海夜未眠 2024-11-02 09:33:45

获取最后一个输入标签的 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>

get the id of last input tag and replave name with that id , below is basic logic

<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>
醉梦枕江山 2024-11-02 09:33:45

您尝试过吗:(

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

Have you tried this :

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);
});  

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

三人与歌 2024-11-02 09:33:45

有几个很好的答案

使用 JavaScript 将光标放置在文本输入元素中的文本末尾

例如,您可以使用

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

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

There are several good answers from

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

For example, you can use

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

Or you can use the JQuery plugin: PutCursorAtEnd. The author even posted the source code there and I tested that this works for Opera.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文