EventTarget是一个由可以接收事件的对象实现的接口,并且可以为它们创建侦听器。

Elementdocumentwindow 是最常见的事件目标,但是其他对象也可以是事件目标,比如XMLHttpRequestAudioNodeAudioContext 等等。

许多事件目标(包括元素,文档和 window)还支持通过 on... 属性和属性设置事件处理程序

构造函数

EventTarget()
创建一个新的 EventTarget 对象实例。

方法

EventTarget.addEventListener()
在EventTarget上注册特定事件类型的事件处理程序。
EventTarget.removeEventListener()
EventTarget中删除事件侦听器。
EventTarget.dispatchEvent()
将事件分派到此EventTarget。

Mozilla chrome 代码的其他方法

Mozilla扩展,供JS实现的事件目标使用以 实现 on* 属性。另见 WebIDL bindings 绑定。

  • void setEventHandler(DOMString type, EventHandler handler)
  • EventHandler getEventHandler(DOMString type)

示例

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 this.removeEventListener(type, callback);
    }
  }
};

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

规范

规范 状态 注释
DOM
EventTarget
Living Standard No change.
Document Object Model (DOM) Level 3 Events Specification
EventTarget
Obsolete A few parameters are now optional (listener), or accepts the null value (useCapture).
Document Object Model (DOM) Level 2 Events Specification
EventTarget
Obsolete Initial definition.

浏览器兼容性

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung Internet
Basic supportChrome Full support 1Edge Full support YesFirefox Full support 1IE Full support 9Opera Full support 7Safari Full support 10.1
Full support 10.1
No support 1 — 10.1
Notes
Notes Window.EventTarget did not exist on versions of Safari before 10.1.
WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 7Safari iOS Full support 10.1
Full support 10.1
No support 1 — 10.1
Notes
Notes Window.EventTarget did not exist on versions of Safari iOS before 10.1.
Samsung Internet Android Full support Yes
EventTarget() constructorChrome Full support 64Edge ? Firefox Full support 59IE No support NoOpera Full support 51Safari No support NoWebView Android Full support 64Chrome Android Full support 64Edge Mobile ? Firefox Android Full support 59Opera Android Full support 51Safari iOS No support NoSamsung Internet Android No support No
addEventListenerChrome Full support 1
Notes
Full support 1
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Edge Full support 12Firefox Full support 1IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.attachEvent() method.
Alternate Name Uses the non-standard name: attachEvent
Opera Full support 7Safari Full support 1WebView Android Full support 1
Notes
Full support 1
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Chrome Android Full support 18
Notes
Full support 18
Notes
Notes Before Chrome 49, the type and listener parameters were optional.
Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 7Safari iOS Full support 1Samsung Internet Android Full support Yes
dispatchEventChrome Full support 4Edge Full support 12Firefox Full support 2IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() method.
Alternate Name Uses the non-standard name: fireEvent
Opera Full support 9Safari Full support 3.2WebView Android Full support 4Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support YesSafari iOS Full support YesSamsung Internet Android Full support Yes
removeEventListenerChrome Full support 1Edge Full support 12Firefox Full support 1IE Full support 9
Full support 9
No support 6 — 11
Notes Alternate Name
Notes Older versions of IE supported an equivalent, proprietary EventTarget.detachEvent() method.
Alternate Name Uses the non-standard name: detachEvent
Opera Full support 7Safari Full support 1WebView Android Full support 1Chrome Android Full support 18Edge Mobile Full support YesFirefox Android Full support 4Opera Android Full support 7Safari iOS Full support 1Samsung Internet Android Full support Yes

Legend

Full support  
Full support
No support  
No support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
Uses a non-standard name.
Uses a non-standard name.

相关链接

文档标签和贡献者

标签: 
此页面的贡献者: zhangchen, Ende93, xgqfrms-GitHub, khalid32, ziyunfei
最后编辑者: zhangchen,