每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

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

<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" onclick="getTxt1CursorPosition()">


</body>


</html>

 

posted @ 2018-12-05 09:12 漫步CODE人生 阅读(...) 评论(...) 编辑 收藏
每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code