eventTarget.addEventListener(type, listener[, options])
type<string>listener<Function> | <EventListener>options<Object>
为 type 事件添加新的句柄。
对于每个 type 和每个 capture 选项值,任何给定的 listener 仅添加一次。
如果 once 选项为 true,则在下一次调度 type 事件后移除 listener。
除了按照 EventTarget 规范跟踪注册的事件监听器之外,Node.js 不会以任何功能方式使用 capture 选项。
具体来说,capture 选项在注册 listener 时用作键的一部分。
任何单独的 listener 都可以与 capture = false 添加一次,与 capture = true 添加一次。
function handler(event) {}
const target = getEventTargetSomehow();
target.addEventListener('foo', handler, { capture: true }); // 第一次
target.addEventListener('foo', handler, { capture: false }); // 第二次
// 删除句柄的第二个实例
target.removeEventListener('foo', handler);
// 删除句柄的第一个实例
target.removeEventListener('foo', handler, { capture: true });type<string>listener<Function> | <EventListener>options<Object>once<boolean> Whentrue, the listener is automatically removed when it is first invoked. Default:false.passive<boolean> Whentrue, serves as a hint that the listener will not call theEventobject'spreventDefault()method. Default:false.capture<boolean> Not directly used by Node.js. Added for API completeness. Default:false.
Adds a new handler for the type event. Any given listener is added
only once per type and per capture option value.
If the once option is true, the listener is removed after the
next time a type event is dispatched.
The capture option is not used by Node.js in any functional way other than
tracking registered event listeners per the EventTarget specification.
Specifically, the capture option is used as part of the key when registering
a listener. Any individual listener may be added once with
capture = false, and once with capture = true.
function handler(event) {}
const target = getEventTargetSomehow();
target.addEventListener('foo', handler, { capture: true }); // first
target.addEventListener('foo', handler, { capture: false }); // second
// Removes the second instance of handler
target.removeEventListener('foo', handler);
// Removes the first instance of handler
target.removeEventListener('foo', handler, { capture: true });