js获取文本域中光标的位置


代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>获取文本域的光标位置</title>
</head>
<body>
<textarea name="" id="textarea" cols="30" rows="10">这是一段文字,每次点击文字内容就会获取当前光标所在的位置</textarea>
<script>
    var oText = document.getElementById('textarea');
     document.onclick = function(e){
         if(e.target.tagName=="TEXTAREA"){
             alert(getCursortPosition(oText));
         }
     }
    function getCursortPosition(obj) {
        var cursorIndex = 0;
        if (document.selection) {
            // IE Support
            obj.focus();
            var range = document.selection.createRange();
            range.moveStart('character', -obj.value.length);
            cursorIndex = range.text.length;
        } else if (obj.selectionStart || obj.selectionStart==0) {
            // another support
            cursorIndex = obj.selectionStart;
        }
        return cursorIndex;
    }
</script>
</body>
</html>

效果:

 


免责声明!

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



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