开发者

Need cursor at beginning of text in textarea

开发者 https://www.devze.com 2023-01-09 12:23 出处:网络
I have this in my body and it works onLoad=\'document.forms.post.message.focus()\' but I need the cursor to be placed in the textarea at the beginning of any existing text, not at the end. 开发者_运维

I have this in my body and it works

onLoad='document.forms.post.message.focus()'

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.


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"]);
0

精彩评论

暂无评论...
验证码 换一张
取 消