blob: 0ed052996263c077a9b934080ccdee20cf7f68fd (
plain)
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
87
88
|
---
title: event.currentTarget
slug: Web/API/Event/currentTarget
tags:
- API
- DOM
- Event
- Property
- Read-only
translation_of: Web/API/Event/currentTarget
---
<p>{{APIRef("DOM")}}</p>
<p>{{domxref("Event")}} 接口的只读属性 <code><strong>currentTarget</strong></code> 表示的,标识是当事件沿着 DOM 触发时事件的当前目标。它总是指向事件绑定的元素,而 {{domxref("Event.target")}} 则是事件触发的元素。</p>
<h2 id="语法">语法</h2>
<pre class="notranslate">var <em>currentEventTarget</em> = <em>event</em>.currentTarget;</pre>
<h3 id="Value">Value</h3>
<p>{{domxref("EventTarget")}}</p>
<h2 id="Example" name="Example">例子</h2>
<p>当将相同的事件处理程序附加到多个元素时 <code>event.currentTarget</code> 就很有用。</p>
<pre class="brush: js notranslate">function hide(e){
e.currentTarget.style.visibility = "hidden";
console.log(e.currentTarget);
// 该函数用作事件处理器时: this === e.currentTarget
}
var ps = document.getElementsByTagName('p');
for(var i = 0; i < ps.length; i++){
// console: 打印被点击的p元素
ps[i].addEventListener('click', hide, false);
}
// console: 打印body元素
document.body.addEventListener('click', hide, false);
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th>Specification</th>
<th>Status</th>
<th>Comment</th>
</tr>
<tr>
<td>{{SpecName("DOM WHATWG", "#dom-event-currenttarget", "Event.currentTarget")}}</td>
<td>{{Spec2("DOM WHATWG")}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName("DOM4", "#dom-event-currenttarget", "Event.currentTarget")}}</td>
<td>{{Spec2("DOM4")}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName("DOM3 Events", "#dfn-current-event-target", "current event target")}}</td>
<td>{{Spec2("DOM3 Events")}}</td>
<td></td>
</tr>
<tr>
<td>{{SpecName("DOM2 Events", "#Events-Event-currentTarget", "Event.currentTarget")}}</td>
<td>{{Spec2("DOM2 Events")}}</td>
<td>Initial definition</td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{Compat("api.Event.currentTarget")}}</p>
<h2 id="参考">参考</h2>
<ul>
<li><a href="/en-US/docs/Web/API/Event/Comparison_of_Event_Targets">Comparison of Event Targets</a></li>
</ul>
|