比如文本框中有abcde,目前光标在最后,索引为5,现在用鼠标点在c后面点一下,索引应该为3,怎么样可以用js获取这个3呢...
<script> function getTxt1CursorPosition(){ var oTxt1 = document.getElementById("txt1"); var cursurPosition=-1; if(oTxt1.selectionStart){//非IE浏览器 cursurPosition= oTxt1.selectionStart; }else{//IE var range = document.selection.createRange(); range.moveStart("character",-oTxt1.value.length); cursurPosition=range.text.length; } alert(cursurPosition); } </script> <input type="text" id="txt1" value="abcde" οnclick="getTxt1CursorPosition()">