--- title: EventTarget slug: Web/API/EventTarget tags: - API - DOM - DOM Events - Interface - NeedsTranslation - TopicStub - 待翻譯 translation_of: Web/API/EventTarget ---

{{ ApiRef("DOM Events") }}

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

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

許多 EventTarget(包括 Element、Document 和 Window)除了透過 {{domxref("EventTarget.addEventListener()", "addEventListener()")}} 方法外,還可藉由 {{domxref("Document_Object_Model", "DOM")}} 物件的屬性({{Glossary("property/JavaScript", "property")}})或 HTML 元素屬性({{Glossary("attribute")}})來設定事件處理器

{{InheritanceDiagram}}

方法

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

Mozilla chrome code 的額外方法

Mozilla extensions for use by JS-implemented event targets to implement on* properties. See also WebIDL bindings.

範例

Simple implementation of EventTarget

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 < 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 < l; i++) {
    stack[i].call(this, event);
  }
  return !event.defaultPrevented;
};

{{ EmbedLiveSample('_Simple_implementation_of_EventTarget') }}

規範

Specification Status Comment
{{SpecName('DOM WHATWG', '#interface-eventtarget', 'EventTarget')}} {{Spec2('DOM WHATWG')}} No change.
{{SpecName('DOM3 Events', 'DOM3-Events.html#interface-EventTarget', 'EventTarget')}} {{Spec2('DOM3 Events')}} A few parameters are now optional (listener), or accepts the null value (useCapture).
{{SpecName('DOM2 Events', 'events.html#Events-EventTarget', 'EventTarget')}} {{Spec2('DOM2 Events')}} Initial definition.

瀏覽器相容性

{{CompatibilityTable}}

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 {{CompatVersionUnknown}} {{CompatGeckoDesktop("1")}} 9.0 7 1.0[1]
Feature Android Edge Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 1.0 {{CompatVersionUnknown}} {{CompatGeckoMobile("1")}} 9.0 6.0 1.0

[1] window.EventTarget does not exist.

參見