js 获取文本框中光标所在字符串索引位置

来源:互联网 发布:如何提高精气神 知乎 编辑:程序博客网 时间:2024/05/21 14:56
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head>  <title> New Document </title>  <meta name="Generator" content="EditPlus">  <meta name="Author" content="">  <meta name="Keywords" content="">  <meta name="Description" content=""> </head> <body>  <script>function getTxt1CursorPosition(){            var $input = document.getElementById("input1");            var cursurPosition=0;            if($input.selectionStart){//非IE                cursurPosition= $input.selectionStart;            }else{//IE                try{                var range = document.selection.createRange();                range.moveStart("character",-$input.value.length);                cursurPosition=range.text.length;                }catch(e){                 cursurPosition = 0;                }            }            alert(cursurPosition);//打印当前索引        }</script><input type="text" id="input1" value="10000" onclick="getTxt1CursorPosition()"> </body></html>
1 0