在contentEditable div中获取插入位置

Ber*_*van 109 javascript caret contenteditable cursor-position

我找到了很多关于如何在contentEditable DIV中设置光标或插入位置的好的,交叉浏览器的答案,但是没有关于如何获取或找到它的位置......

我想要做的是在关键字中知道该div中插入符号的位置.

因此,当用户输入文本时,我可以随时知道其光标在div中的位置.

编辑:我在div内容(文本)中寻找INDEX,而不是光标坐标.

<div id="contentBox" contentEditable="true"></div>

$('#contentbox').keyup(function() { 
    // ... ? 
});
Run Code Online (Sandbox Code Playgroud)

Tim*_*own 112

以下代码假定:

  • 可编辑区域内始终存在单个文本节点,<div>而其他节点中没有
  • 可编辑的div没有white-space设置CSS 属性pre

码:

function getCaretPosition(editableDiv) {
  var caretPos = 0,
    sel, range;
  if (window.getSelection) {
    sel = window.getSelection();
    if (sel.rangeCount) {
      range = sel.getRangeAt(0);
      if (range.commonAncestorContainer.parentNode == editableDiv) {
        caretPos = range.endOffset;
      }
    }
  } else if (document.selection && document.selection.createRange) {
    range = document.selection.createRange();
    if (range.parentElement() == editableDiv) {
      var tempEl = document.createElement("span");
      editableDiv.insertBefore(tempEl, editableDiv.firstChild);
      var tempRange = range.duplicate();
      tempRange.moveToElementText(tempEl);
      tempRange.setEndPoint("EndToEnd", range);
      caretPos = tempRange.text.length;
    }
  }
  return caretPos;
}
Run Code Online (Sandbox Code Playgroud)
#caretposition {
  font-weight: bold;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="contentbox" contenteditable="true">Click me and move cursor with keys or mouse</div>
<div id="caretposition">0</div>
<script>
  var update = function() {
    $('#caretposition').html(getCaretPosition(this));
  };
  $('#contentbox').on("mousedown mouseup keydown keyup", update);
</script>
Run Code Online (Sandbox Code Playgroud)

  • 如果那里有任何其他标签,这将无效.问题:如果插入符号位于`<div>`中的`<a>`元素内,那么你想要什么偏移?`<a>`里面文本中的偏移量? (8认同)
  • @ giorgio79:是的,因为换行符会产生一个`<br>`或`<div>`元素,这违反了答案中提到的第一个假设.如果您需要更通用的解决方案,可以尝试http://stackoverflow.com/a/4812022/96100 (5认同)
  • @Richard:嗯,'keyup`可能是错误的事件,但是原始问题中使用的是.`getCaretPosition()`本身在其自身的限制范围内是好的. (3认同)
  • 如果我按下回车键并继续换行,那JSFIDDLE演示会失败.该职位将显示0. (3认同)
  • 无论如何有做,所以它包括行号? (2认同)

mwa*_*wag 20

在其他答案中我看不到一些皱纹:

  1. 元素可以包含多个级别的子节点(例如,具有具有子节点的子节点的子节点...)
  2. 一个选择可以包含不同的开始和结束位置(例如,选择了多个字符)
  3. 包含插入符号开始/结束的节点可能不是元素或其直接子元素

这是一种获取开始位置和结束位置作为元素的textContent值的偏移量的方法:

// node_walk: walk the element tree, stop when func(node) returns false
function node_walk(node, func) {
  var result = func(node);
  for(node = node.firstChild; result !== false && node; node = node.nextSibling)
    result = node_walk(node, func);
  return result;
};

// getCaretPosition: return [start, end] as offsets to elem.textContent that
//   correspond to the selected portion of text
//   (if start == end, caret is at given position and no text is selected)
function getCaretPosition(elem) {
  var sel = window.getSelection();
  var cum_length = [0, 0];

  if(sel.anchorNode == elem)
    cum_length = [sel.anchorOffset, sel.extentOffset];
  else {
    var nodes_to_find = [sel.anchorNode, sel.extentNode];
    if(!elem.contains(sel.anchorNode) || !elem.contains(sel.extentNode))
      return undefined;
    else {
      var found = [0,0];
      var i;
      node_walk(elem, function(node) {
        for(i = 0; i < 2; i++) {
          if(node == nodes_to_find[i]) {
            found[i] = true;
            if(found[i == 0 ? 1 : 0])
              return false; // all done
          }
        }

        if(node.textContent && !node.firstChild) {
          for(i = 0; i < 2; i++) {
            if(!found[i])
              cum_length[i] += node.textContent.length;
          }
        }
      });
      cum_length[0] += sel.anchorOffset;
      cum_length[1] += sel.extentOffset;
    }
  }
  if(cum_length[0] <= cum_length[1])
    return cum_length;
  return [cum_length[1], cum_length[0]];
}
Run Code Online (Sandbox Code Playgroud)

  • 必须选择正确的答案。它与文本内的标签一起使用(接受的响应无效) (2认同)
  • 这是正确答案,应标记为正确答案 (2认同)

小智 17

$("#editable").on('keydown keyup mousedown mouseup',function(e){
		   
       if($(window.getSelection().anchorNode).is($(this))){
    	  $('#position').html('0')
       }else{
         $('#position').html(window.getSelection().anchorOffset);
       }
 });
Run Code Online (Sandbox Code Playgroud)
body{
  padding:40px;
}
#editable{
  height:50px;
  width:400px;
  border:1px solid #000;
}
#editable p{
  margin:0;
  padding:0;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.1/jquery.min.js"></script>
<div contenteditable="true" id="editable">move the cursor to see position</div>
<div>
position : <span id="position"></span>
</div>
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,一旦你按下Enter键并且从另一条线开始就停止工作(它再次从0开始 - 可能从CR/LF开始计算). (3认同)
  • 如果您有一些粗体和/或斜体字,它就不能正常工作。 (2认同)

J.Y*_*Han 14

试试这个:

Caret.js从文本字段获取插入符号位置和偏移量

https://github.com/ichord/Caret.js

演示:http: //ichord.github.com/Caret.js

  • @AndroidDev对不起男人,但我不是Caret.js的作者. (13认同)

小智 10

window.getSelection - vs - document.selection

这个对我有用:

function getCaretCharOffset(element) {
  var caretOffset = 0;

  if (window.getSelection) {
    var range = window.getSelection().getRangeAt(0);
    var preCaretRange = range.cloneRange();
    preCaretRange.selectNodeContents(element);
    preCaretRange.setEnd(range.endContainer, range.endOffset);
    caretOffset = preCaretRange.toString().length;
  } 

  else if (document.selection && document.selection.type != "Control") {
    var textRange = document.selection.createRange();
    var preCaretTextRange = document.body.createTextRange();
    preCaretTextRange.moveToElementText(element);
    preCaretTextRange.setEndPoint("EndToEnd", textRange);
    caretOffset = preCaretTextRange.text.length;
  }

  return caretOffset;
}


// Demo:
var elm = document.querySelector('[contenteditable]');
elm.addEventListener('click', printCaretPosition)
elm.addEventListener('keydown', printCaretPosition)

function printCaretPosition(){
  console.log( getCaretCharOffset(elm), 'length:', this.textContent.trim().length )
}
Run Code Online (Sandbox Code Playgroud)
<div contenteditable>some text here <i>italic text here</i> some other text here <b>bold text here</b> end of text</div>
Run Code Online (Sandbox Code Playgroud)

调用行取决于事件类型,对于关键事件,请使用:

getCaretCharOffsetInDiv(e.target) + ($(window.getSelection().getRangeAt(0).startContainer.parentNode).index());
Run Code Online (Sandbox Code Playgroud)

对于鼠标事件使用这个:

getCaretCharOffsetInDiv(e.target.parentElement) + ($(e.target).index())
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,我通过添加目标索引来处理中断线


Chr*_*van 8

由于我花了很长时间才弄清楚如何使用新的window.getSelection API,因此我将与后代分享。请注意,MDN 建议对 window.getSelection 提供更广泛的支持,但是,您的情况可能会有所不同。

const getSelectionCaretAndLine = () => {
    // our editable div
    const editable = document.getElementById('editable');

    // collapse selection to end
    window.getSelection().collapseToEnd();

    const sel = window.getSelection();
    const range = sel.getRangeAt(0);

    // get anchor node if startContainer parent is editable
    let selectedNode = editable === range.startContainer.parentNode
      ? sel.anchorNode 
      : range.startContainer.parentNode;

    if (!selectedNode) {
        return {
            caret: -1,
            line: -1,
        };
    }

    // select to top of editable
    range.setStart(editable.firstChild, 0);

    // do not use 'this' sel anymore since the selection has changed
    const content = window.getSelection().toString();
    const text = JSON.stringify(content);
    const lines = (text.match(/\\n/g) || []).length + 1;

    // clear selection
    window.getSelection().collapseToEnd();

    // minus 2 because of strange text formatting
    return {
        caret: text.length - 2, 
        line: lines,
    }
} 
Run Code Online (Sandbox Code Playgroud)

这是一个在键盘上触发的jsfiddle 。但请注意,快速方向键按下以及快速删除似乎是跳过事件。


Sou*_*uet 7

金达晚了晚会,但万一其他人都在挣扎。在过去两天中,我发现的所有Google搜索都没有找到任何可行的方法,但是我提出了一种简洁而优雅的解决方案,无论您拥有多少个嵌套标签,该解决方案都将始终有效:

cursor_position() {
    var sel = document.getSelection();
    sel.modify("extend", "backward", "paragraphboundary");
    var pos = sel.toString().length;
    console.log('pos: '+pos);
    if(sel.anchorNode != undefined) sel.collapseToEnd();

    return pos;
}
Run Code Online (Sandbox Code Playgroud)

它一直选择到段落的开头,然后计算字符串的长度以获取当前位置,然后撤消选择以将光标返回到当前位置。如果要对整个文档(一个以上的段落)执行此操作,请根据您的情况更改paragraphboundarydocumentboundary或任意粒度。查看API以获取更多详细信息。干杯! :)

  • 看起来 selection.modify 可能会或可能不会被所有浏览器支持。https://developer.mozilla.org/en-US/docs/Web/API/Selection (4认同)

Nis*_* Up 5

function getCaretPosition() {
    var x = 0;
    var y = 0;
    var sel = window.getSelection();
    if(sel.rangeCount) {
        var range = sel.getRangeAt(0).cloneRange();
        if(range.getClientRects()) {
        range.collapse(true);
        var rect = range.getClientRects()[0];
        if(rect) {
            y = rect.top;
            x = rect.left;
        }
        }
    }
    return {
        x: x,
        y: y
    };
}
Run Code Online (Sandbox Code Playgroud)

  • 这返回像素位置,而不是字符偏移量 (2认同)