blob: 7fcc81f4b3645f437e01e8827bd4e9af00a34bee (
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
89
90
91
92
93
94
95
96
97
98
99
|
---
title: Event.currentTarget
slug: Web/API/Event/currentTarget
translation_of: Web/API/Event/currentTarget
---
<p>{{APIRef("DOM")}}</p>
<p>Identifica l'elemento del DOM a cui è stato assegnato l'event handler diversamente dall' <code>event.target</code> che rappresenta l'elemento da cui è stato generato l'evento.</p>
<h2 id="Esempio">Esempio</h2>
<p>L' <code>event.currentTarget </code>risulta utile da usare quando si assegna lo stesso event handler a numerosi elementi.</p>
<pre class="brush: js">function hide(e){
e.currentTarget.style.visibility = "hidden";
// When this function is used as an event handler: this === e.currentTarget
}
var ps = document.getElementsByTagName('p');
for(var i = 0; i < ps.length; i++){
ps[i].addEventListener('click', hide, false);
}
// click around and make paragraphs disappear
</pre>
<h2 id="Specifiche">Specifiche</h2>
<table class="standard-table">
<tbody>
<tr>
<th>Specification</th>
<th>Status</th>
<th>Comment</th>
</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="Compatibilità_Browser">Compatibilità Browser</h2>
<p>{{CompatibilityTable}}</p>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatVersionUnknown}}<sup>[1]</sup></td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<p>[1] Il modello degli eventi implementato dagli Internet Explorer da 6 a 8 è diverso. Gli event listerner sono attaccati attraverso il metodo non standard <code>element.attachEvent. </code>In questo modello non esiste un equivalente dell' <code>event.currentTarget</code> inoltre <code>this</code> è l'oggetto globale. Una possibile soluzione è quella di wrappare la gestione dell' <code>event.currentTarget </code>in una funzione che chiami la funzione di handler attraverso la <code>Function.prototype.call</code> passando come primo parametro l'elemento. In questo modo il valore associato allo <code>this</code> è quello aspettato.</p>
<h2 id="Vedi_anche">Vedi anche</h2>
<p><a href="/en-US/docs/Web/API/Event/Comparison_of_Event_Targets">Comparison of Event Targets</a></p>
|