如何将光标移动到 JavaScript 中文本区域的最后一个位置?

发布于 2024-09-07 11:23:27 字数 80 浏览 11 评论 0原文

我在表单上有一个文本区域和一个按钮。文本区域中可能已经有一些文本。我希望单击按钮时光标移动到文本区域中的最后一个位置。

这可能吗?

I have a textarea and a button on a form. The textarea may already have some text in it. I would like the cursor to move to the last position in the text area when the button is clicked.

Is this possible?

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

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

发布评论

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

评论(7

司马昭之心 2024-09-14 11:23:27

xgMz 的答案最适合我。您无需担心浏览器:

var html = $("#MyTextArea").val();
$("#MyTextArea").focus().val("").val(html);

这是我编写的一个快速 jQuery 扩展,下次可以为我执行此操作:

; (function($) {
    $.fn.focusToEnd = function() {
        return this.each(function() {
            var v = $(this).val();
            $(this).focus().val("").val(v);
        });
    };
})(jQuery);

像这样使用:

$("#MyTextArea").focusToEnd();

xgMz's answer was best for me. You don't need to worry about the browser:

var html = $("#MyTextArea").val();
$("#MyTextArea").focus().val("").val(html);

And here's a quick jQuery extension I wrote to do this for me next time:

; (function($) {
    $.fn.focusToEnd = function() {
        return this.each(function() {
            var v = $(this).val();
            $(this).focus().val("").val(v);
        });
    };
})(jQuery);

Use like this:

$("#MyTextArea").focusToEnd();
独守阴晴ぅ圆缺 2024-09-14 11:23:27

“最后一个位置”是指文本的结尾吗?

更改表单字段的“.value”会将光标移动到除 IE 之外的所有浏览器中的结尾。使用 IE,您必须亲自动手并使用非标准接口故意操作选择:

    if (browserIsIE) {
        var range= element.createTextRange();
        range.collapse(false);
        range.select();
    } else {
        element.focus();
        var v= element.value();
        element.value= '';
        element.value= v;
    }

或者您的意思是把光标放回到上次文本区域聚焦时的位置? >

在除 IE 之外的每个浏览器中,只需调用 'element.focus()' 就可以实现这一点;浏览器会记住每个输入的最后一个光标/选择位置并将其放回到焦点上。

在 IE 中重现这将是相当棘手的。您必须始终轮询以检查光标/选择的位置,并记住该位置(如果它位于输入元素中),然后在按下按钮时获取给定元素的位置并恢复它。这涉及到对“document.selection.createRange()”的一些非常繁琐的操作。

我不知道 jQuery 中有什么可以帮助你做到这一点,但也许某个地方有一个插件?

By “the last position”, do you mean the end of the text?

Changing the ‘.value’ of a form field will move the cursor to the end in every browser except IE. With IE you have to get your hands dirty and deliberately manipulate the selection, using non-standard interfaces:

    if (browserIsIE) {
        var range= element.createTextRange();
        range.collapse(false);
        range.select();
    } else {
        element.focus();
        var v= element.value();
        element.value= '';
        element.value= v;
    }

Or do you mean put the cursor back to the place it was previously, the last time the textarea was focused?

In every browser except IE, this will already happen just by calling ‘element.focus()’; the browser remembers the last cursor/selection position per input and puts it back on focus.

This would be quite tricky to reproduce in IE. You would have to poll all the time to check where the cursor/selection was, and remember that position if it was in an input element, then fetch the position for a given element when the button was pressed and restore it. This involves some really quite tedious manipulation of ‘document.selection.createRange()’.

I'm not aware of anything in jQuery that would help you do this, but there might be a plugin somewhere perhaps?

舞袖。长 2024-09-14 11:23:27

您也可以这样做:

$(document).ready(function()
{
    var el = $("#myInput").get(0);

    var elemLen = el.value.length;

    el.selectionStart = elemLen;
    el.selectionEnd = elemLen;

    el.focus();
});​

此代码适用于 inputtextarea。使用最新浏览器进行测试:Firefox 23、IE 11 和 Chrome 28。

jsFiddle 可在此处获取: http://jsfiddle.net/cG9gN/1/

来源:https://stackoverflow.com/a/12654402/ 114029

You can also do this:

$(document).ready(function()
{
    var el = $("#myInput").get(0);

    var elemLen = el.value.length;

    el.selectionStart = elemLen;
    el.selectionEnd = elemLen;

    el.focus();
});​

This code works for both input and textarea. Tested with latest browsers: Firefox 23, IE 11 and Chrome 28.

jsFiddle available here: http://jsfiddle.net/cG9gN/1/

Source: https://stackoverflow.com/a/12654402/114029

楠木可依 2024-09-14 11:23:27

您可以使用这个 https://github.com/DrPheltRight/jquery-caret

$('input').caretToEnd();

You could use this https://github.com/DrPheltRight/jquery-caret

$('input').caretToEnd();
溺ぐ爱和你が 2024-09-14 11:23:27

这是我在 onclick 或其他事件中用于文本区域的方法,并且似乎适用于 FireFox 和 IE 7 & 浏览器。 8. 它将光标定位在文本的末尾而不是前面。

this.value = "some text I want to pre-populate";
this.blur();
this.focus();

This is what I use for textareas inside my onclick or other events and seems to work fine for FireFox and IE 7 & 8. It positions the cursor at the end of the text instead of the front.

this.value = "some text I want to pre-populate";
this.blur();
this.focus();
灰色世界里的红玫瑰 2024-09-14 11:23:27

设置鼠标焦点并将光标移动到输入末尾。它适用于每个浏览器,这里只是简单的逻辑。

input.focus();
tmpStr = input.val();
input.val('');
input.val(tmpStr);

To set mouse focus and move cursor to end of input. Its work in every browser, Its just simple logic here.

input.focus();
tmpStr = input.val();
input.val('');
input.val(tmpStr);
时光瘦了 2024-09-14 11:23:27

我稍微修改了 @mkaj 扩展以支持默认值

; (function($) {
    $.fn.focusToEnd = function(defaultValue) {
        return this.each(function() {
            var prevValue = $(this).val();
            if (typeof prevValue == undefined || prevValue == "") {
                prevValue = defaultValue;
            }
            $(this).focus().val("").val(prevValue);
        });
    };
})(jQuery);

// See it in action here
$(element).focusToEnd("Cursor will be at my tail...");

I modified @mkaj extension a little to support default value

; (function($) {
    $.fn.focusToEnd = function(defaultValue) {
        return this.each(function() {
            var prevValue = $(this).val();
            if (typeof prevValue == undefined || prevValue == "") {
                prevValue = defaultValue;
            }
            $(this).focus().val("").val(prevValue);
        });
    };
})(jQuery);

// See it in action here
$(element).focusToEnd("Cursor will be at my tail...");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文