Range: phương thức comparePoint()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2018.

Phương thức comparePoint() của giao diện Range xác định liệu một điểm được chỉ định nằm trước, trong hay sau Range. Điểm được xác định bởi một nút tham chiếu và một độ lệch trong nút đó.

Cú pháp

js
comparePoint(referenceNode, offset)

Tham số

referenceNode

Nodeoffset được tính theo.

offset

Một số nguyên lớn hơn hoặc bằng 0 mô tả vị trí bên trong referenceNode của điểm cần kiểm tra. Nếu referenceNodeNode kiểu Text, Comment, hoặc CDATASection, thì offset là số ký tự tính từ đầu referenceNode. Với các kiểu Node khác, offset là số nút con tính từ đầu referenceNode.

Giá trị trả về

Một số.

  • -1 nếu điểm được chỉ định bởi referenceNodeoffset nằm trước đầu Range này.
  • 0 nếu điểm được chỉ định bởi referenceNodeoffset nằm trong Range này.
  • 1 nếu điểm được chỉ định bởi referenceNodeoffset nằm sau cuối Range này.

Ví dụ

js
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

Thông số kỹ thuật

Thông số kỹ thuật
DOM
# ref-for-dom-range-comparepoint①

Tương thích trình duyệt

Xem thêm