TextArea 获取当前光标位置并可再当前位置插入内容

var CodeArea = document.getElementById('CodeArea');
var res = GetSelectedText(CodeArea, 'index');//传index获取当前位置

ChangeSelectedText(CodeArea, str);//str传值




//获取光标位置,或者选中内容

        function GetSelectedText(obj, type) {
            debugger;
            var userSelection;
            if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
                // 非IE浏览器
                // 获取选区的开始位置
                var startPos = obj.selectionStart,
                          // 获取选区的结束位置
                    endPos = obj.selectionEnd;
                //console.log("非IE:")
                //console.log("选区开始点:" + startPos + ',选区结束点:' + endPos)
                if (type == "index") {
                    if (obj.selectionStart == obj.textLength) {
                        userSelection = "*#文本末尾#*";
                    }
                } else {
                    userSelection = obj.value.substring(startPos, endPos)
                }
            } else if (document.selection) {
                // IE浏览器
                console.log("IE:")
                userSelection = document.selection.createRange().text
            }

            return userSelection
        }
        //在光标位置插入新的内容
        function ChangeSelectedText(obj, str) {
            if (window.getSelection) {
                // 非IE浏览器
                //console.log("非IE:")
                obj.setRangeText(str);
                //  在未选中文本的情况下,重新设置光标位置
                obj.selectionStart += str.length;
                obj.focus()
            } else if (document.selection) {
                // IE浏览器
                //console.log("IE:")
                obj.focus();
                var sel = document.selection.createRange();
                sel.text = str;
            }
        }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
textarea 光标位置插入是指在文本区域(textarea)中的光标位置插入新的文本或内容。 要实现在指定位置插入文本,可以通过以下步骤进行: 1. 获取文本区域的光标位置,可以使用textarea元素的selectionStart和selectionEnd属性来获取。 2. 将要插入的文本与原有文本分成两个段落,即左侧和右侧。 3. 将左侧段落与要插入的文本拼接起来,拼接右侧段落,形成新的文本。 4. 将新的文本重新赋给文本区域。 以下是一个简单的示例代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Textarea光标位置插入示例</title> <script> function insertText() { var textarea = document.getElementById("myTextarea"); var position = textarea.selectionStart; var textToInsert = "新的文本"; var originalText = textarea.value; var newText = originalText.slice(0, position) + textToInsert + originalText.slice(position); textarea.value = newText; } </script> </head> <body> <textarea id="myTextarea"></textarea> <button onclick="insertText()">在光标位置插入</button> </body> </html> ``` 在上述示例中,我们通过获取textarea元素的selectionStart属性获得光标位置,并使用slice函数将原有文本按照光标位置拆分成左右两个段落。接着,我们将新的文本插入到左侧段落中,并次拼接上右侧段落,最后将新的文本赋给textarea的value属性即可。 这样,当点击按钮时,就能在光标位置插入指定的新文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值