Howto Place cursor at beginning of textarea

后端 未结 5 1054
有刺的猬
有刺的猬 2021-02-04 12:46

I\'ve found a couple resources for how to place the cursor in a textarea at the end of the text, but I can\'t sort out a simple way to make it appear at the beginning.

5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-04 13:24

    Pass a reference to your textarea to this JS function.

    function resetCursor(txtElement) { 
        if (txtElement.setSelectionRange) { 
            txtElement.focus(); 
            txtElement.setSelectionRange(0, 0); 
        } else if (txtElement.createTextRange) { 
            var range = txtElement.createTextRange();  
            range.moveStart('character', 0); 
            range.select(); 
        } 
    }
    

提交回复
热议问题