在textarea中需要光标在文本的开头
问题描述:
我有这个在我的身体,它的工作原理
I have this in my body and it works
onLoad='document.forms.post.message.focus()'
但是我需要将光标放在任何现有文本开头的textarea中,而不是在末尾。这就把它放在最后。
but I need the cursor to be placed in the textarea at the beginning of any existing text, not at the end. This puts it at the end.
请注意,我对JavaScript一无所知,所以请保持温和。
Note that I know nothing about JavaScript, so be gentle.
谢谢
答
function moveCaretToStart(el) {
if (typeof el.selectionStart == "number") {
el.selectionStart = el.selectionEnd = 0;
} else if (typeof el.createTextRange != "undefined") {
el.focus();
var range = el.createTextRange();
range.collapse(true);
range.select();
}
}
moveCaretToStart(document.forms["post"].elements["message"]);