[英]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();
}
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.