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>

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM