selectionStart-End with textareas

前端 未结 2 1233
猫巷女王i
猫巷女王i 2020-12-01 10:14

I\'m having this annoying problem, I can\'t seem to get the starting and ending index of the selected text in a textarea, all I get is undefined like this:

$         


        
2条回答
  •  囚心锁ツ
    2020-12-01 11:12

    Try:

    $('#myarea')[0].selectionStart;
    

    Why? A jQuery selector does not return the actual DOM elements but the wrapped jQuery collection. jQuery makes the actual DOM elements accessible as an array, so if you wanted to use the 1st matched element (and in this case, the only one, since it's by ID), you would do the above.

提交回复
热议问题