javascript what does range.endOffset() give you?

江枫思渺然 提交于 2019-12-25 16:43:01

问题


When I go:

range.selectNodeContents(element);

My understanding is that the range object selects all the contents of element. So that would mean that if I said:

range.startOffset();

I should get 0. Which is true when testing. But then if I say:

range.endOffset();

I would think that I get the length of the contents of the element. But I don't. I get 1, or 3... or numbers I don't understand.

So... what does range.endOffset() really tell you?


回答1:


The Range.endOffset read-only property returns a number representing where in the Range.endContainer the Range ends.

From: https://developer.mozilla.org/en-US/docs/Web/API/Range/endOffset




回答2:


When you use selectNodeContents,

  1. Let length be the length of node.

  1. Set end to the boundary point (node, length).

So the end offset is set to the length of the node:

To determine the length of a node node, switch on node:

  • DocumentType
    Zero.

  • Text, ProcessingInstruction, Comment
    The number of code units in its data.

  • Any other node
    Its number of children.

That's what you will get you you call endOffset.



来源:https://stackoverflow.com/questions/39812835/javascript-what-does-range-endoffset-give-you

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!