elementFromPoint() method—available on both the Document and ShadowRoot objects—returns the topmost 元素 at the specified coordinates (relative to the viewport).

If the element at the specified point belongs to another document (for example, the document of an <iframe> ), that document's parent element is returned (the <iframe> itself). If the element at the given point is anonymous or XBL generated content, such as a textbox's scroll bars, then the first non-anonymous ancestor element (for example, the textbox) is returned.

Elements with pointer-events 设为 none will be ignored, and the element below it will be returned.

If the method is run on another document (like an <iframe> 's subdocument), the coordinates are relative to the document where the method is being called.

If the specified point is outside the visible bounds of the document or either coordinate is negative, the result is null .

If you need to find the specific position inside the element, use Document.caretPositionFromPoint() .

句法

const element = document.elementFromPoint(x, y)
					

参数

x
The horizontal coordinate of a point, relative to the left edge of the current viewport .
y

The vertical coordinate of a point, relative to the top edge of the current viewport.

返回值

The topmost 元素 object located at the specified coordinates.

范例

This example creates two buttons which let you set the current color of the paragraph element located under the coordinates (2, 2) .

JavaScript

function changeColor(newColor) {
  elem = document.elementFromPoint(2, 2);
  elem.style.color = newColor;
}
					

changeColor() method simply obtains the element located at the specified point, then sets that element's current foreground color property to the color specified by the newColor 参数。

HTML

<p id="para1">Some text here</p>
<button onclick="changeColor('blue');">Blue</button>
<button onclick="changeColor('red');">Red</button>
					

The HTML provides the paragraph whose color will be affected, as well as two buttons: one to change the color to blue, and another to change the color to red.

结果

规范

规范 状态
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
elementFromPoint 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 Firefox 63 IE Yes Opera 40 Safari Yes 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 Yes 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 .

图例

完整支持

完整支持

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

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

见实现注意事项。

另请参阅

元数据

  • 最后修改: