Set cursor position in an input text field

后端 未结 2 1455
失恋的感觉
失恋的感觉 2021-01-01 18:45

After a lot of search I found the following threads:

define cursor position in form input field

jQuery Set Cursor Position in Text Area

Unfortunately

2条回答
  •  Happy的楠姐
    2021-01-01 19:06

    I think I found the error in your setCursor method. The moveStart and moveEnd methods expect two arguments, the first being the unit it should use. Also, the end position appears to be relative to the start position. So I think instead of

        textRange.moveEnd(pos);
        textRange.moveStart(pos);
    

    you want

        textRange.moveStart('character', pos);
        textRange.moveEnd('character', 0);
    

    See: http://msdn.microsoft.com/en-us/library/ie/ms536623(v=vs.85).aspx

提交回复
热议问题