问题描述:

First, selection usually achieved this way:

const sel = window.getSelection()

From that, some values can be retrieved:

const { anchorNode, focusNode, anchorOffset, focusOffset } = sel

const range = sel.getRangeAt(0)

const { startOffset, endOffset, startContainer, endContainer } = range

Now, these value are equivalent:

// in forward selection

anchorNode === startContainer

anchorOffset === startOffset

focusNode === endContainer

focusOffset === endOffset

// in backward selection

anchorNode === endContainer

anchorOffset === endOffset

focusNode === startContainer

focusOffset === startOffset

// in no selection (just placing cursor)

// all '===' above is 'true'

I'm planning to use getRange. Because I don't care (in my project) about

selection direction. But i'm not sure, because maybe there are some differences

or quirks I didn't know.

So, which one is the preferred one?

相关阅读:
Top