获取光标位置

原文:https://blog.csdn.net/mafan121/article/details/78519348

html

<iframe id="editor"  width="600px" height="400px" style="border:solid 1px;"></iframe>
<input type="txt" onclick="alert(getPosition(event.target))"/>

js

    //初始化编辑器
    function init() {
        var ifr = document.getElementById("editor");
        var doc = ifr.contentDocument || ifr.contentWindow.document; // W3C || IE
        doc.open();
        doc.designMode="on";
        doc.contentEditable = true;
        doc.write(‘<html><head><style>body{margin:3px; word-wrap:break-word; word-break: break-all; }</style></head><body>GoodNessEditor</body></html>‘);
        doc.close();
        console.log(doc.body.innerHTML);
        doc.addEventListener(‘click‘,function(e){
            alert(getDivPosition(e));
        });
    }
    init();
    //可编辑div获取坐标
    var getDivPosition = function (event) {
        var element = event.target ?  event.target : event;
        var caretOffset = 0;
        var doc = element.ownerDocument || element.document;
        var win = doc.defaultView || doc.parentWindow;
        var sel;
        if (typeof win.getSelection != "undefined") {//谷歌、火狐
            sel = win.getSelection();
            if (sel.rangeCount > 0) {//选中的区域
                var range = win.getSelection().getRangeAt(0);
                var preCaretRange = range.cloneRange();//克隆一个选中区域
                preCaretRange.selectNodeContents(element);//设置选中区域的节点内容为当前节点
                preCaretRange.setEnd(range.endContainer, range.endOffset);  //重置选中区域的结束位置
                caretOffset = preCaretRange.toString().length;
            }
        } else if ((sel = doc.selection) && sel.type != "Control") {//IE
            var textRange = sel.createRange();
            var preCaretTextRange = doc.body.createTextRange();
            preCaretTextRange.moveToElementText(element);
            preCaretTextRange.setEndPoint("EndToEnd", textRange);
            caretOffset = preCaretTextRange.text.length;
        }
        return caretOffset;
    }
    //输入框获取光标
    var getPosition = function (event) {
        var element = event.target ?  event.target : event;
        let cursorPos = 0;
        if (document.selection) {//IE
            var selectRange = document.selection.createRange();
            selectRange.moveStart(‘character‘, -element.value.length);
            cursorPos = selectRange.text.length;
        } else if (element.selectionStart || element.selectionStart == ‘0‘) {
            cursorPos = element.selectionStart;
        }
        return cursorPos;
    }

原文地址:https://www.cnblogs.com/bruce-gou/p/9882892.html

时间: 2024-10-08 12:20:09

获取光标位置的相关文章

android EditText获取光标位置并安插字符删除字符

android EditText获取光标位置并插入字符删除字符1.获取光标位置int index = editText.getSelectionStart(); 2.在光标处插入字符int index = editText.getSelectionStart();Editable editable = editText.getText();editable.insert(index, "aaaa");3.删除光标前字符int index = editText.getSelectionS

关于文本输入框获取光标位置以及指定位置插入内容

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> </head> <body> &l

android EditText获取光标位置并且插入字符或者删除字符

1.获取光标位置 [java] int index = editText.getSelectionStart(); int index = editText.getSelectionStart(); 2.在光标处插入字符 [java] int index = editText.getSelectionStart(); Editable editable = editText.getText(); editable.insert(index, "aaaa"); int index = e

javastscript获取光标位置

需求是获取某元素的内容,然后将该内容插入到文本框当前的光标位置 (function($) { $.fn.extend({ insertAtCaret: function(myValue) { var $t = $(this)[0]; if (document.selection) { this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); } else if ($t.selec

可编辑div 获取光标位置及插入内容

<!DOCTYPE html> <html> <title>Div 光标位置 插入 文字 或 HTML</title> <script language="javascript" type="text/javascript"> var range; function getPos() { range = window.getSelection().getRangeAt(0);//找到焦点位置 var spa

UITextView获取光标位置

UITextRange *range = textView.selectedTextRange; //光标位置 CGRect  rect = [textView caretRectForPosition:range.start]; 当UITextView的光标在最后位置连续输入4个或以上换行符号时,输出rect为 {{inf,inf},{0,0}}

Android EditText获取光标位置并插入字符删除字符

1.获取到光标所在的位置 int index = editText.getSelectionStart(); 2.在光标的地方插入字符 int index = editText.getSelectionStart(); Editable editable = editText.getText(); editable.insert(index, "哈哈哈"); 3.删除光标前的字符(光标在哪里从哪里开始删除) int index = editText.getSelectionStart(

jquery扩展获取光标位置

(function($){ $.fn.extend({ insertAtCaret: function(myValue){ var $t=$(this)[0]; if (document.selection) { this.focus(); sel = document.selection.createRange(); sel.text = myValue; this.focus(); } else if ($t.selectionStart || $t.selectionStart == '0

javascript获取以及设置光标位置

一. 获取光标位置: // 获取光标位置 function getCursortPosition (textDom) { var cursorPos = 0; if (document.selection) { // IE Support textDom.focus (); var selectRange = document.selection.createRange(); selectRange.moveStart ('character', -textDom.value.length);