1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
---
title: DocumentOrShadowRoot.elementFromPoint()
slug: Web/API/Document/elementFromPoint
translation_of: Web/API/DocumentOrShadowRoot/elementFromPoint
original_slug: Web/API/DocumentOrShadowRoot/elementFromPoint
---
<p>{{APIRef("Shadow DOM")}}{{SeeCompatTable}}</p>
<p><span class="seoSummary">{{domxref("DocumentOrShadowRoot")}} 接口的 <strong><code>elementFromPoint()</code></strong> 方法返回给定坐标点下最上层的 {{domxref('element')}} 元素。 </span></p>
<p>If the element at the specified point belongs to another document (for example, an iframe's subdocument), the subdocument'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.</p>
<p>如果指定的坐标点在文档的可视范围外,或者两个坐标都是负数,那么结果返回 <code>null</code>。</p>
<p>If you need to find the specific position inside the element, use {{domxref("Document.caretPositionFromPoint()")}}.</p>
<h2 id="Syntax">Syntax</h2>
<pre class="syntaxbox notranslate">var element = document.elementFromPoint(x, y);</pre>
<h3 id="Parameters">Parameters</h3>
<dl>
<dt>x</dt>
<dd>坐标点的横坐标。</dd>
<dt>y</dt>
<dd>坐标点的纵坐标。</dd>
</dl>
<h3 id="Returns">Returns</h3>
<p>在给定的坐标点处的顶端 {{domxref("Element")}}(译者注:如果元素层叠的话,返回最上层的元素)。</p>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:html notranslate" id="ExampleCode"><!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>
</pre>
<h3 id="Demo">Demo</h3>
<p>{{ EmbedLiveSample('Example', '', '', '', 'Web/API/Document/elementFromPoint') }}</p>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('Shadow DOM','#extensions-to-the-documentorshadowroot-mixin','DocumentOrShadowRoot')}}</td>
<td>{{Spec2('Shadow DOM')}}</td>
<td>Initial definition.</td>
</tr>
</tbody>
</table>
<h2 id="Browser_Compatibility">Browser Compatibility</h2>
<div>
<p>{{Compat("api.DocumentOrShadowRoot.elementFromPoint")}}</p>
</div>
|