简体   繁体   English

如何在文本区域的末尾设置光标?

[英]How to set cursor at the end in a textarea?

Is there a way to set the cursor at the end in a textarea element?有没有办法在textarea元素的末尾设置光标? I'm using Firefox 3.6 and I don't need it to work in IE or Chrome.我使用的是 Firefox 3.6,我不需要它在 IE 或 Chrome 中工作。 It seems all the related answers in here use onfocus() event, which seems to be useless because when user clicks on anywhere within the textarea element Firefox sets cursor position to there.似乎这里的所有相关答案都使用onfocus()事件,这似乎没用,因为当用户点击textarea元素中的任何地方时,Firefox 会将光标位置设置到那里。 I have a long text to display in a textarea so that it displays the last portion (making it easier to add something at the end).我有一个长文本要显示在textarea以便它显示最后一部分(使其更容易在最后添加一些内容)。

No frameworks or libraries.没有框架或库。

There may be many ways, eg可能有很多方法,例如

element.focus();
element.setSelectionRange(element.value.length,element.value.length);

http://jsfiddle.net/doktormolle/GSwfW/ http://jsfiddle.net/doktormolle/GSwfW/

It's been a long time since I used javascript without first looking at a jQuery solution...自从我在没有首先查看 jQuery 解决方案的情况下使用 javascript 以来,已经有很长时间了......

That being said, your best approach using javascript would be to grab the value currently in the textarea when it comes into focus and set the value of the textarea to the grabbed value.话虽如此,使用 javascript 的最佳方法是在 textarea 成为焦点时抓取当前在 textarea 中的值,并将 textarea 的值设置为抓取的值。 This always works in jQuery as:这在 jQuery 中始终有效,因为:

$('textarea').focus(function() {
    var theVal = $(this).val();
    $(this).val(theVal);
});

In plain javascript:在普通的javascript中:

var theArea = document.getElementByName('[textareaname]');

theArea.onFocus = function(){
    var theVal = theArea.value;
    theArea.value = theVal;
}

I could be wrong.我可能是错的。 Bit rusty.有点生锈。

Here is a function for that这是一个函数

function moveCaretToEnd(el) {
    if (typeof el.selectionStart == "number") {
        el.selectionStart = el.selectionEnd = el.value.length;
    } else if (typeof el.createTextRange != "undefined") {
        el.focus();
        var range = el.createTextRange();
        range.collapse(false);
        range.select();
    }
}

[Demo] [Source] [演示] [来源]

var t = /* get textbox element */ ;

t.onfocus = function () { 
    t.scrollTop = t.scrollHeight; 
    setTimeout(function(){ 
      t.select(); 
      t.selectionStart = t.selectionEnd; 
    }, 10); 
}

The trick is using the setTimeout to change the text insertion (carat) position after the browser is done handling the focus event;诀窍是在浏览器处理完焦点事件使用 setTimeout 更改文本插入(克拉)位置; otherwise the position would be set by our script and then immediately set to something else by the browser.否则位置将由我们的脚本设置,然后立即由浏览器设置为其他内容。

textarea.focus()
textarea.value+=' ';//adds a space at the end, scrolls it into view
(this.jQuery || this.Zepto).fn.focusEnd = function () {
    return this.each(function () {
        var val = this.value;
        this.focus();
        this.value = '';
        this.value = val;
    });
};

selectionStart is enough to set initial cursor point. selectionStart 足以设置初始光标点。

    element.focus();
    element.selectionStart = element.value.length;

@Dr.Molle answer is right. @Dr.Molle 回答是对的。 just for enhancement, U can combine with prevent-default.只是为了增强,U 可以与 prevent-default 结合。

http://jsfiddle.net/70des6y2/ http://jsfiddle.net/70des6y2/

Sample:样本:

document.getElementById("textarea").addEventListener("mousedown", e => {
  e.preventDefault();
  moveToEnd(e.target);
});
function moveToEnd(element) {
  element.focus();
  element.setSelectionRange(element.value.length, element.value.length);
}

如何将“ctrl+end”键发送到<textarea>&lt;i&gt;element so the cursor lands after last character&lt;/div&gt;&lt;/i&gt;&lt;b&gt;元素,所以 cursor 在最后一个字符之后着陆&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> (发送:即模拟用户通常会键入这些键)</p><p> 我知道这个方法 element.value = element.value,但我认为对于其中包含大量数据的文本区域(如 wiki 等)可能会很慢。</p></div> - How to send the 'ctrl+end' key to <textarea> element so the cursor lands after last character

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将光标设置为在Firefox上损坏的textareasetSelectionRange()的结尾? - Set cursor to end of textarea- setSelectionRange() broken on Firefox? 如何在 ReactJs 中的 Textarea 内设置光标位置? - How to set cursor position inside Textarea in ReactJs? 如何在文本区域的光标开始和结束之间获取文本 - how to get text in between cursor start and end of textarea 将光标设置在字符串的末尾? - set the cursor at the end of the string? jQuery-将光标放在文本区域的末尾 - JQuery - placing the cursor at the end of a textarea when it is focused on 添加文本后,将光标置于文本区域的末尾 - Put cursor at the end of textarea after adding text 具有valueBinding的Ember TextArea导致光标跳至textarea的末尾 - Ember TextArea with valueBinding is causing the cursor to jump to the end of the textarea 如何将“ctrl+end”键发送到<textarea>&lt;i&gt;element so the cursor lands after last character&lt;/div&gt;&lt;/i&gt;&lt;b&gt;元素,所以 cursor 在最后一个字符之后着陆&lt;/div&gt;&lt;/b&gt;</textarea><div id="text_translate"><p> (发送:即模拟用户通常会键入这些键)</p><p> 我知道这个方法 element.value = element.value,但我认为对于其中包含大量数据的文本区域(如 wiki 等)可能会很慢。</p></div> - How to send the 'ctrl+end' key to <textarea> element so the cursor lands after last character 如何让 textarea 的 cursor 在拖放时移动到突出显示的文本的末尾? - How to get textarea's cursor move to the end of the highlighted text on drag and drop? 如何自动在textarea中填充光标? - How to automatically populate cursor in textarea?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM