Document: elementsFromPoint()-Methode

Baseline Widely available

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

Die elementsFromPoint()-Methode des Document-Interfaces gibt ein Array von allen Elementen an den angegebenen Koordinaten (relativ zum Viewport) zurück. Die Elemente sind vom obersten bis zum untersten Kasten des Viewports geordnet.

Sie funktioniert in ähnlicher Weise wie die elementFromPoint()-Methode.

Syntax

js
elementsFromPoint(x, y)

Parameter

x

Die horizontale Koordinate eines Punktes.

y

Die vertikale Koordinate eines Punktes.

Rückgabewert

Ein Array von Element-Objekten, geordnet vom obersten bis zum untersten Kasten des Viewports.

Beispiele

HTML

html
<div>
  <p>Some text</p>
</div>
<p>Elements at point 30, 20:</p>
<div id="output"></div>

JavaScript

js
let output = document.getElementById("output");
if (document.elementsFromPoint) {
  let elements = document.elementsFromPoint(30, 20);
  elements.forEach((elt, i) => {
    output.textContent += elt.localName;
    if (i < elements.length - 1) {
      output.textContent += " < ";
    }
  });
} else {
  output.innerHTML = `<span style="color: red">
  Browser does not support
  <code>document.elementsFromPoint()</code>
</span>
`;
}

Spezifikationen

Specification
CSSOM View Module
# dom-document-elementsfrompoint

Browser-Kompatibilität

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
elementsFromPoint

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
See implementation notes.
Requires a vendor prefix or different name for use.
Has more compatibility info.

Siehe auch