大网数据广告活动
Web 标准与前端开发

Range: comparePoint() method

· 52CE 我爱测

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.

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 offset is relative to.
  • offset
    • : An integer greater than or equal to zero describing the position inside referenceNode of the point to be checked. If referenceNode is a Node of type Text, Comment, or CDATASection, then offset is the number of characters from the start of referenceNode. For other Node types, offset is the number of child nodes from the start of the referenceNode.

Return value

A number.

  • -1 if the point specified by the referenceNode and offset is before the start of this Range.
  • 0 if the point specified by the referenceNode and offset is within this Range (including the start and end points of the range).
  • 1 if the point specified by the referenceNode and offset is after the end of this Range.

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。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。