选区操作 #js
var range = window.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
// console.log(range.startOffset);
const startOffset = range.startOffset;
preCaretRange.setStart(range.startContainer, startOffset);
preCaretRange.setEnd(range.endContainer, startOffset + 1);
const caretOffset = preCaretRange.toString();
console.log(caretOffset);
var range = window.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
// console.log(range.startOffset);
const start = range.startOffset;
let authorTextElem = range.endContainer;
preCaretRange.setStart(range.startContainer, 0);
preCaretRange.setEnd(range.endContainer, preCaretRange.endOffset);
let text = preCaretRange.toString();
while (authorTextElem.previousSibling && authorTextElem.previousSibling.nodeType === 3 && text.indexOf('@') === -1) {
authorTextElem = authorTextElem.previousSibling;
text = authorTextElem.nodeValue + text;
}
console.log(text);
const selection = document.getSelection();
const oRange = selection.getRangeAt(0); //get the text range
const oRect = oRange.getBoundingClientRect();
var el = document.getElementById("editable");
var range = document.createRange();
var sel = window.getSelection();
range.setStart(el.childNodes[2], 5);
range.collapse(true);
sel.removeAllRanges();
sel.addRange(range);