• 获取Textarea 元素当前的光标位置及document.selection.createRange()资料


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Demo : Textarea 元素的光标位置</title>
        <style type="text/css">
            #result
            {
                font-size: 18px;
                line-height: 25px;
                padding-left: 20px;
            }
        </style>
    </head>
    <body>
        <div>
            <h1>
                Textarea 元素的光标位置</h1>
            <ul>
                <li>获取 Textarea 元素当前的光标位置</li>
                <li>设置回原先的 Textarea 元素的光标位置</li>
                <li>在 Textarea 元素的光标位置插入文本</li>
            </ul>
            <form action="#">
            <textarea id="test" rows="8" cols="50"></textarea>
            <p>
                <input type="button" id="get" value="Get Cursor Position" />
                <input type="button" id="set" value="Set Cursor Position" />
                <input type="button" id="add" value="Add Text After Cursor Position" />
            </p>
            </form>
            <h2>
                Textarea Range:</h2>
            <div id="result">
            </div>
            <script type="text/javascript">
    
                document.getElementById("get").onclick = function () {
                    //alert(tx.value.length);
                    var tx = document.getElementById("test");
                    var re = document.getElementById("result");
                    var pos = cursorPosition.get(tx);
                    re.innerHTML = ("<strong>Range :</strong> (" + pos.start + ", " + pos.end + ")<br /><strong>Text :</strong> " + (!pos.text ? '//--' : pos.text));
                }
    
                document.getElementById("set").onclick = function () {
                    var tx = document.getElementById("test");
                    var re = document.getElementById("result");
                    var pos = cursorPosition.get(tx);
                    cursorPosition.set(tx, pos);
                    re.innerHTML = "";
                }
    
                document.getElementById("add").onclick = function () {
                    var tx = document.getElementById("test");
                    pos = cursorPosition.get(tx);
                    cursorPosition.add(tx, pos, input = prompt("你想插入替换的文本:", ""));
                }
    
                /**
                * cursorPosition Object
                */
                var cursorPosition = {
                    get: function (textarea) {
                        var rangeData = { text: "", start: 0, end: 0 };
    
                        if (textarea.setSelectionRange) { // W3C	
                            textarea.focus();
                            rangeData.start = textarea.selectionStart;
                            rangeData.end = textarea.selectionEnd;
                            rangeData.text = (rangeData.start != rangeData.end) ? textarea.value.substring(rangeData.start, rangeData.end) : "";
                        } else if (document.selection) { // IE
                            textarea.focus();
                            var i,
    				oS = document.selection.createRange(),
                            // Don't: oR = textarea.createTextRange()
    				oR = document.body.createTextRange();
                            oR.moveToElementText(textarea);
    
                            rangeData.text = oS.text;
                            rangeData.bookmark = oS.getBookmark();
    
                            // object.moveStart(sUnit [, iCount]) 
                            // Return Value: Integer that returns the number of units moved.
                            for (i = 0; oR.compareEndPoints('StartToStart', oS) < 0 && oS.moveStart("character", -1) !== 0; i++) {
                                // Why? You can alert(textarea.value.length)
                                if (textarea.value.charAt(i) == '\r') {
                                    i++;
                                }
                            }
                            rangeData.start = i;
                            rangeData.end = rangeData.text.length + rangeData.start;
                        }
    
                        return rangeData;
                    },
    
                    set: function (textarea, rangeData) {
                        var oR, start, end;
                        if (!rangeData) {
                            alert("You must get cursor position first.")
                        }
                        textarea.focus();
                        if (textarea.setSelectionRange) { // W3C
                            textarea.setSelectionRange(rangeData.start, rangeData.end);
                        } else if (textarea.createTextRange) { // IE
                            oR = textarea.createTextRange();
    
                            // Fixbug : ues moveToBookmark()
                            // In IE, if cursor position at the end of textarea, the set function don't work
                            if (textarea.value.length === rangeData.start) {
                                //alert('hello')
                                oR.collapse(false);
                                oR.select();
                            } else {
                                oR.moveToBookmark(rangeData.bookmark);
                                oR.select();
                            }
                        }
                    },
    
                    add: function (textarea, rangeData, text) {
                        var oValue, nValue, oR, sR, nStart, nEnd, st;
                        this.set(textarea, rangeData);
    
                        if (textarea.setSelectionRange) { // W3C
                            oValue = textarea.value;
                            nValue = oValue.substring(0, rangeData.start) + text + oValue.substring(rangeData.end);
                            nStart = nEnd = rangeData.start + text.length;
                            st = textarea.scrollTop;
                            textarea.value = nValue;
                            // Fixbug:
                            // After textarea.values = nValue, scrollTop value to 0
                            if (textarea.scrollTop != st) {
                                textarea.scrollTop = st;
                            }
                            textarea.setSelectionRange(nStart, nEnd);
                        } else if (textarea.createTextRange) { // IE
                            sR = document.selection.createRange();
                            sR.text = text;
                            sR.setEndPoint('StartToEnd', sR);
                            sR.select();
                        }
                    }
                }
            </script>
        </div>
    </body>
    </html>
    


     

  • 相关阅读:
    [转]Sublime Text 3安装Json格式化插件
    Golang 新手可能会踩的 50 个坑【转】
    [golang]svg图片默认按照左上角旋转,改为按中心旋转,重新计算中心偏移量
    序列化是干什么的,有什么作用,什么情况下会用到?
    Hbase设置多个hmaster
    基于JMX动态配置Log4J日志级别
    面向过程与面向对象编程的区别和优缺点
    log4j自带的两个类MDC和NDC作用以及用途
    【架构师之路】集群/分布式环境下5种session处理策略
    Java Web项目如何做到升级不断掉服务,同时涉及到的相关问题
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234435.html
Copyright © 2020-2023  润新知