Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Document.elementFromPoint()

概述

返回当前文档上处于指定坐标位置最顶层的元素, 坐标是相对于包含该文档的浏览器窗口的左上角为原点来计算的, 通常 x 和 y 坐标都应为正数.

语法

var element = document.elementFromPoint(x, y);
  • element 是返回的DOM元素.
  • xy 是坐标数值, 不需要单位比如px.

示例

<!DOCTYPE html>
<html lang="en">
<head>
<title>elementFromPoint example</title>

<script>
function changeColor(newColor) {
  elem = document.elementFromPoint(2, 2);
  elem.style.color = newColor;
}
</script>
</head>

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

附注

If the element at the specified point belongs to another document (for example, an iframe's subdocument), the element in the DOM of the document the method is called on (in the iframe case, the iframe itself) is returned. 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.

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

Note: Callers from XUL documents should wait until the onload event has fired before calling this method.

规范

文档标签和贡献者

 此页面的贡献者: teoli, ziyunfei
 最后编辑者: ziyunfei,