CaretPosition: getClientRect() Methode

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Die getClientRect() Methode der CaretPosition Schnittstelle gibt das Client-Rechteck für den Caret-Bereich zurück.

Syntax

js
getClientRect()

Parameter

Keine.

Rückgabewert

Ein DOMRect Objekt.

Beispiele

Die Bildschirmposition des Carets bestimmen

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)})`);
});

Spezifikationen

Specification
CSSOM View Module
# dom-caretposition-getclientrect

Browser-Kompatibilität

Siehe auch