这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

elementsFromPoint() 方法在 DocumentOrShadowRoot interface returns an array of all elements at the specified coordinates (relative to the viewport).

It operates in a similar way to the elementFromPoint() 方法。

句法

const elements = document.elementsFromPoint(x, y);
					

参数

x

The horizontal coordinate of a point.

y

The vertical coordinate of a point.

返回值

An array of element 对象。

范例

HTML

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

JavaScript

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

规范

规范 状态
Shadow DOM
The definition of 'elementsFromPoint()' in that specification.
过时
CSSOM (CSS 对象模型) 视图模块
The definition of 'elementsFromPoint()' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
elementsFromPoint Chrome 53
53
Before Chrome 66, this method returned null when the element was a child of a host node. See issue 759947 .
Edge 12 Alternate Name
12 Alternate Name
返回 NodeList instead of an array. See the MSDN documentation 。返回 null when the point provided has no elements beneath it (e.g., when given a point outside the document).
Alternate Name Uses the non-standard name: msElementsFromPoint
Firefox 63 IE 10 Alternate Name
10 Alternate Name
返回 NodeList instead of an array. See the MSDN documentation 。返回 null when the point provided has no elements beneath it (e.g., when given a point outside the document).
Alternate Name Uses the non-standard name: msElementsFromPoint
Opera 40 Safari 12 WebView Android 53
53
Before WebView 66, this method returned null when the element was a child of a host node. See issue 759947 .
Chrome Android 53
53
Before Chrome 66, this method returned null when the element was a child of a host node. See issue 759947 .
Firefox Android 63 Opera Android 41 Safari iOS 12 Samsung Internet Android 6.0
6.0
Before Samsung Internet 9.0, this method returned null when the element was a child of a host node. See issue 759947 .

图例

完整支持

完整支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

使用非标名称。

另请参阅

元数据

  • 最后修改: