DOM
The comparePoint() method of the Range interface determines whether a specified point is before, within, or after the Range. The point is specified by a reference node and an offset within that node.
Syntax
comparePoint(referenceNode, offset)
Parameters
referenceNode- : The Node that the
offsetis relative to.
- : The Node that the
offset- : An integer greater than or equal to zero describing the position inside
referenceNodeof the point to be checked. IfreferenceNodeis a Node of type Text, Comment, or CDATASection, thenoffsetis the number of characters from the start ofreferenceNode. For other Node types,offsetis the number of child nodes from the start of thereferenceNode.
- : An integer greater than or equal to zero describing the position inside
Return value
A number.
-1if the point specified by thereferenceNodeandoffsetis before the start of thisRange.0if the point specified by thereferenceNodeandoffsetis within thisRange(including the start and end points of the range).1if the point specified by thereferenceNodeandoffsetis after the end of thisRange.
Examples
const text = new Text("0123456789");
const thisRange = new Range();
thisRange.setStart(text, 1);
thisRange.setEnd(text, 6);
thisRange.comparePoint(text, 3); // 0
thisRange.comparePoint(text, 0); // -1
thisRange.comparePoint(text, 6); // 0
thisRange.comparePoint(text, 7); // 1
Specifications
Browser compatibility
See also
来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。
