Need cursor at beginning of text in textarea

后端 未结 1 718
眼角桃花
眼角桃花 2021-02-09 18:29

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 b

1条回答
  •  日久生厌
    2021-02-09 18:36

    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 讨论(0)
提交回复
热议问题