Skip to content
Go back

highlighter maker

Published:  at  23:29

On this page

https://juejin.cn/post/6844903827745832967

https://juejin.cn/post/6927490083348348936

https://juejin.cn/post/7234779572759625788

https://juejin.cn/post/7140078451205079054

https://juejin.cn/post/6870058781527506952

https://zhuanlan.zhihu.com/p/163479419

核心问题

{
 collapsed: false,
 endContainer: text, // nodeType 3
 endOffset: 5,
 startContainer: text, // nodeType 3
 startOffset: 2,
}

childNodes 返回节点

children 返回元素

splitText

入参:要中断文本节点之前的索引

返回:节点包含指定偏移点之后的文本

<p>Document.createRange()</p>
<p><m>Document</m>.createRange()</p>
<p><m>Do<m>cu
</m>ment</m>.createRange()</p>

$0 的 childNodes 有1个,即:$0.childNodes[0] 为 Document.createRange()

执行 $0.childNodes[0].splitText(2)

$0 的 childNodes 有 2 个

$0.childNodes[0] 为 Do

$0.childNodes[1] 为 cument.createRange()

获取选中的 textNode

// 选中 me
<p>Document.createRange()</p>
// 选中 d R
<span>method</span> <span>Range objects</span>
// 选中 eD
<div>
 the
 <p>Document</p>
</div>

标识 DOM 节点:

{tagName, index, childIndex}

问题:

都会改变 childNodes,导致定位不准确


Suggest Changes

Previous Post
javascript transpiler
Next Post
memory leak

Most Related Posts

  • low code

    Published:  at  20:30

    low code

  • css

    Published:  at  20:39

    css

  • codec in js

    Published:  at  17:00

    codec in js

  • baidu map

    Published:  at  23:29

    baidu map

  • javascript scope

    Published:  at  13:14

    javascript scope