js 获取输入框中光标的索引位置

it2022-05-23  64

<html> <head></head> <body> <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()"> </body> </html>

 

转载于:https://www.cnblogs.com/scode2/p/10069033.html

相关资源:数据结构—成绩单生成器

最新回复(0)