aboutsummaryrefslogtreecommitdiff
path: root/files/zh-tw/web/api/eventtarget/index.html
blob: 4c10aa133e0d15ed20df83f26c84874ff50cfd3c (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
---
title: EventTarget
slug: Web/API/EventTarget
tags:
  - API
  - DOM
  - DOM Events
  - Interface
  - NeedsTranslation
  - TopicStub
  - 待翻譯
translation_of: Web/API/EventTarget
---
<p>{{ ApiRef("DOM Events") }}</p>

<p><strong><code>EventTarget</code></strong> 介面定義了其實作的物件具有接收事件的能力,也可能擁有處理事件的監聽器。</p>

<p>除了最為常見的 {{domxref("Element")}}{{domxref("Document")}} 與 {{domxref("Window")}} 繼承或實作了 <code>EventTarget</code> 介面之外,其它的物件還有 {{domxref("XMLHttpRequest")}}{{domxref("AudioNode")}}{{domxref("AudioContext")}}⋯等等。</p>

<p>許多 <code>EventTarget</code>(包括 Element、Document 和 Window)除了透過 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} 方法外,還可藉由 {{domxref("Document_Object_Model", "DOM")}} 物件的屬性({{Glossary("property/JavaScript", "property")}})或 <a href="/zh-TW/docs/Web/HTML/Element">HTML 元素</a>屬性({{Glossary("attribute")}})來設定<a href="/zh-TW/docs/Web/Guide/DOM/Events/Event_handlers">事件處理器</a></p>

<p>{{InheritanceDiagram}}</p>

<h2 id="方法" name="方法">方法</h2>

<dl>
 <dt>{{domxref("EventTarget.addEventListener()")}}</dt>
 <dd><code>EventTarget</code> 物件上註冊指定事件的監聽器。</dd>
 <dt>{{domxref("EventTarget.removeEventListener()")}}</dt>
 <dd>移除 <code>EventTarget</code> 物件上的指定事件監聽器。</dd>
 <dt>{{domxref("EventTarget.dispatchEvent()")}}</dt>
 <dd>對此 <code>EventTarget</code> 物件派送(dispatch)一個事件物件,也就是於此 <code>EventTarget</code> 物件上觸發一個指定的事件物件實體。</dd>
</dl>

<h3 id="Mozilla_chrome_code_的額外方法">Mozilla chrome code 的額外方法</h3>

<p>Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also <a href="/docs/Mozilla/WebIDL_bindings">WebIDL bindings</a>.</p>

<ul>
 <li>void <strong>setEventHandler</strong>(DOMString type, EventHandler handler) {{non-standard_inline}}</li>
 <li>EventHandler <strong>getEventHandler</strong>(DOMString type) {{non-standard_inline}}</li>
</ul>

<h2 id="範例">範例</h2>

<h3 id="_Simple_implementation_of_EventTarget" name="_Simple_implementation_of_EventTarget">Simple implementation of EventTarget</h3>

<pre class="brush: js">var EventTarget = function() {
  this.listeners = {};
};

EventTarget.prototype.listeners = null;
EventTarget.prototype.addEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    this.listeners[type] = [];
  }
  this.listeners[type].push(callback);
};

EventTarget.prototype.removeEventListener = function(type, callback) {
  if (!(type in this.listeners)) {
    return;
  }
  var stack = this.listeners[type];
  for (var i = 0, l = stack.length; i &lt; l; i++) {
    if (stack[i] === callback){
      stack.splice(i, 1);
      return;
    }
  }
};

EventTarget.prototype.dispatchEvent = function(event) {
  if (!(event.type in this.listeners)) {
    return true;
  }
  var stack = this.listeners[event.type];
  event.target = this;
  for (var i = 0, l = stack.length; i &lt; l; i++) {
    stack[i].call(this, event);
  }
  return !event.defaultPrevented;
};
</pre>

<p>{{ EmbedLiveSample('_Simple_implementation_of_EventTarget') }}</p>

<h2 id="Specification" name="Specification">規範</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('DOM WHATWG', '#interface-eventtarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM WHATWG')}}</td>
   <td>No change.</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM3 Events', 'DOM3-Events.html#interface-EventTarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM3 Events')}}</td>
   <td>A few parameters are now optional (<code>listener</code>), or accepts the <code>null</code> value (<code>useCapture</code>).</td>
  </tr>
  <tr>
   <td>{{SpecName('DOM2 Events', 'events.html#Events-EventTarget', 'EventTarget')}}</td>
   <td>{{Spec2('DOM2 Events')}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_Compatibility" name="Browser_Compatibility">瀏覽器相容性</h2>

<p>{{CompatibilityTable}}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Chrome</th>
   <th>Edge</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>1.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("1")}}</td>
   <td>9.0</td>
   <td>7</td>
   <td>1.0<sup>[1]</sup></td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>Android</th>
   <th>Edge</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>1.0</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("1")}}</td>
   <td>9.0</td>
   <td>6.0</td>
   <td>1.0</td>
  </tr>
 </tbody>
</table>
</div>

<p>[1] <code>window.EventTarget</code> does not exist.</p>

<h2 id="參見">參見</h2>

<ul>
 <li><a href="/docs/Web/Reference/Events">Event reference</a> - the events available in the platform.</li>
 <li><a href="/docs/Web/Guide/DOM/Events">Event developer guide</a></li>
 <li>{{domxref("Event")}} interface</li>
</ul>