zoukankan      html  css  js  c++  java
  • 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>

    效果:

  • 相关阅读:
    php 匿名函数和闭包
    项目在线压缩js
    USACOTrainning.The Clocks
    USACOTrainning.Mother's Milk
    c# TXT文件读写
    Access以及少量Regex
    USACOTraining.Packing Rectangles
    First
    CUGBLinker and EXE
    异常处理总结
  • 原文地址:https://www.cnblogs.com/mankii/p/11010629.html
Copyright © 2011-2022 走看看