markdown 选区操作#js

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown 选区操作#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);
```

以上是关于markdown 选区操作#js的主要内容,如果未能解决你的问题,请参考以下文章

PS选区认识

gimp 和 PS 选区操作方法

GIMP模板选区操作

markdown JS操作符||,&&

移动工具V和选区工具M

PS--工具类