EventTarget

EventTarget is a DOM interface implemented by objects that can receive events and may have listeners for them.

Element , Document ,和 Window are the most common event targets, but other objects can be event targets, too. For example XMLHttpRequest , AudioNode , AudioContext , and others.

Many event targets (including elements, documents, and windows) also support setting event handlers via on event properties and attributes.

构造函数

EventTarget()
创建新的 EventTarget object instance.

方法

EventTarget .addEventListener()
Registers an event handler of a specific event type on the EventTarget .
EventTarget .removeEventListener()
Removes an event listener from the EventTarget .
EventTarget .dispatchEvent()
Dispatches an event to this EventTarget .

Additional methods in Mozilla chrome codebase

Mozilla includes a couple of extensions for use by JS-implemented event targets to implement on event 特性。

另请参阅 WebIDL bindings .

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

范例

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

规范

规范 状态 Comment
DOM
The definition of 'EventTarget' in that specification.
实时标准 No change.
Document Object Model (DOM) Level 3 Events Specification
The definition of 'EventTarget' in that specification.
Obsolete A few parameters are now optional ( listener ), or accepts the null value ( useCapture ).
Document Object Model (DOM) Level 2 Events Specification
The definition of 'EventTarget' in that specification.
Obsolete 初始定义。

浏览器兼容性

更新 GitHub 上的兼容性数据
Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
EventTarget Chrome 1 Edge 12 Firefox 1 IE 6 Opera 7 Safari 1
注意事项
1
注意事项
window.EventTarget did not exist on versions of Safari before 10.1.
WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1
注意事项
1
注意事项
window.EventTarget did not exist on versions of Safari iOS before 10.3.
Samsung Internet Android 1.0
EventTarget() 构造函数 Chrome 64 Edge ≤79 Firefox 59 IE 不支持 No Opera 51 Safari 不支持 No WebView Android 64 Chrome Android 64 Firefox Android 59 Opera Android 47 Safari iOS 不支持 No Samsung Internet Android 9.0
addEventListener Chrome 1
注意事项
1
注意事项
Before Chrome 49, the type and listener parameters were optional.
Edge 12 Firefox 1 IE 9
9
6 — 11
注意事项 Alternate Name
Older versions of IE supported an equivalent, proprietary EventTarget.attachEvent() 方法。
Alternate Name Uses the non-standard name: attachEvent
Opera 7 Safari 1 WebView Android 1
注意事项
1
注意事项
Before Chrome 49, the type and listener parameters were optional.
Chrome Android 18
注意事项
18
注意事项
Before Chrome 49, the type and listener parameters were optional.
Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0
注意事项
1.0
注意事项
Before Samsung Internet 5.0, the type and listener parameters were optional.
dispatchEvent Chrome 4 Edge 12 Firefox 2 IE 9
9
6 — 11
注意事项 Alternate Name
Older versions of IE supported an equivalent, proprietary EventTarget.fireEvent() 方法。
Alternate Name Uses the non-standard name: fireEvent
Opera 9 Safari 3.2 WebView Android 4 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 3 Samsung Internet Android 1.0
removeEventListener Chrome 1 Edge 12 Firefox 1 IE 9
9
6 — 11
注意事项 Alternate Name
Older versions of IE supported an equivalent, proprietary EventTarget.detachEvent() 方法。
Alternate Name Uses the non-standard name: detachEvent
Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持
完整支持
不支持
不支持
见实现注意事项。
使用非标名称。
使用非标名称。

另请参阅