CaretPosition: phương thức getClientRect()

Baseline 2025
Newly available

Since December 2025, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Phương thức getClientRect() của giao diện CaretPosition trả về hình chữ nhật client cho phạm vi của con trỏ soạn thảo.

Cú pháp

js
getClientRect()

Tham số

Không có.

Giá trị trả về

Một đối tượng DOMRect.

Ví dụ

Lấy vị trí trên màn hình của con trỏ soạn thảo

html
<input aria-label="text field" value="Click inside this input field" />
js
document.querySelector("input").addEventListener("click", (event) => {
  const x = event.clientX;
  const y = event.clientY;

  const caret = document.caretPositionFromPoint?.(x, y);
  if (!caret) {
    log("Not supported");
    return;
  }

  const rect = caret.getClientRect();

  log(`Caret bounding rect: ${JSON.stringify(rect)}`);
  log(`Caret is at (${rect.x.toFixed(2)}, ${rect.y.toFixed(2)})`);
});

Thông số kỹ thuật

Thông số kỹ thuật
CSSOM View Module
# dom-caretposition-getclientrect

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

Xem thêm