CustomEvent interface represents events initialized by an application for any purpose.

注意: 此特征可用于 Web 工作者 .

构造函数

CustomEvent()
创建 CustomEvent .

特性

CustomEvent.detail 只读

Any data passed when initializing the event.

This interface inherits properties from its parent, 事件 :

Event.bubbles 只读

布尔指示事件是否透过 DOM 冒泡。

Event.cancelBubble
历史别名为 Event.stopPropagation() 。把其值设为 true 在从事件处理程序返回之前阻止事件的传播。
Event.cancelable 只读

布尔指示事件是否可取消。

Event.composed 只读

A boolean indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.

Event.currentTarget 只读
A reference to the currently registered target for the event. This is the object to which the event is currently slated to be sent. It's possible this has been changed along the way through retargeting .
Event.deepPath
数组 of DOM 节点 s through which the event has bubbled.
Event.defaultPrevented 只读
Indicates whether or not the call to event.preventDefault() canceled the event.
Event.eventPhase 只读

Indicates which phase of the event flow is being processed.

Event.explicitOriginalTarget 只读

The explicit original target of the event (Mozilla-specific.)

Event.originalTarget 只读

The original target of the event, before any retargetings. (Mozilla-specific.)

Event.returnValue
A historical property introduced by Internet Explorer and eventually adopted into the DOM specification in order to ensure existing sites continue to work. Ideally, you should try to use Event.preventDefault() and Event.defaultPrevented instead, but you can use returnValue if you choose to do so.
Event.srcElement
A non-standard alias (from old versions of Microsoft Internet Explorer) for Event.target . Some other browsers are starting to support it for web compatibility purposes.
Event.target 只读

A reference to the target to which the event was originally dispatched.

Event.timeStamp 只读
The time at which the event was created (in milliseconds). By specification, this value is time since epoch—but in reality, browsers' definitions vary. In addition, work is underway to change this to be a DOMHighResTimeStamp 代替。
Event.type 只读

The name of the event. Case-insensitive.

Event.isTrusted 只读
Indicates whether or not the event was initiated by the browser (after a user click, for instance) or by a script (using an event creation method, like Event.initEvent ).

过时特性

Event.scoped 只读
Obsolete; use composed instead. (A 布尔 indicating whether the given event will bubble across through the shadow root into the standard DOM.)

方法

CustomEvent.initCustomEvent()

初始化 CustomEvent object. If the event has already being dispatched, this method does nothing.

This interface inherits methods from its parent, 事件 :

Event.createEvent()

创建新事件,然后必须被初始化通过调用其 initEvent() 方法。

Event.composedPath()
Returns the event’s path (objects on which listeners will be invoked). This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.
Event.initEvent()

初始化创建事件的值。若事件已被分派,此方法什么都不做。

Event.preventDefault()

取消事件 (若它可取消)。

Event.stopImmediatePropagation()

For this particular event, prevent all other listeners from being called. This includes listeners attached to the same element as well as those attached to elements that will be traversed later (during the capture phase, for instance).

Event.stopPropagation()

停止在 DOM 中进一步传播事件。

过时方法

Event.getPreventDefault()
Non-standard; use Event.defaultPrevented instead. (Returns the value of Event.defaultPrevented )。
Event.preventBubble() 从 Gecko 24 起已过时
Obsolete; use event.stopPropagation instead. (Prevents the event from bubbling.)
Event.preventCapture() 从 Gecko 24 起已过时
Obsolete; use event.stopPropagation 代替。

规范

规范 状态 注释
DOM
The definition of 'CustomEvent' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
CustomEvent Chrome 15 Edge 12 Firefox 6 IE 9 Opera 11 Safari 5.1 WebView Android ≤37 Chrome Android 18 Firefox Android 6 Opera Android 11 Safari iOS 6 Samsung Internet Android 1.0
CustomEvent() 构造函数 Chrome 15 Edge ≤18 Firefox 11 IE No Opera 11.6 Safari 6.1 WebView Android ≤37 Chrome Android 18 Firefox Android 14 Opera Android 12 Safari iOS 6.1 Samsung Internet Android 1.0
detail Chrome 15 Edge 14 Firefox 11 IE No Opera 11.6 Safari 6.1 WebView Android Yes Chrome Android Yes Firefox Android 14 Opera Android Yes Safari iOS 6.1 Samsung Internet Android Yes
initCustomEvent 弃用 Chrome Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Edge 14 Firefox 6 IE 9 Opera 11 Safari 5.1 WebView Android Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Chrome Android Yes
Yes
59
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Firefox Android 6 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes
Yes
7.0
canBubble , cancelable ,和 detail are optional parameters defaulting to false , false ,和 null 分别。
Available in workers Chrome Yes Edge 12 Firefox 48 IE Yes Opera Yes Safari Yes WebView Android Yes Chrome Android Yes Firefox Android 48 Opera Android Yes Safari iOS Yes Samsung Internet Android Yes

图例

完整支持

完整支持

不支持

不支持

弃用。不要用于新网站。

弃用。不要用于新网站。

见实现注意事项。

Firing from privileged code to non-privileged code

When firing a CustomEvent from privileged code (i.e. an extension) to non-privileged code (i.e. a webpage), security issues should be considered. Firefox and other Gecko applications restrict an object created in one context from being directly used for another, which will automatically prevent security holes, but these restrictions may also prevent your code from running as expected.

While creating a CustomEvent object, you must create the object from the same window detail attribute of your CustomEvent will be subjected to the same restrictions. 字符串 and 数组 values will be readable by the content without restrictions, but custom 对象 will not. While using a custom object, you will need to define the attributes of that object that are readable from the content script using Components.utils.cloneInto() .

// doc is a reference to the content document
function dispatchCustomEvent(doc) {
  var eventDetail = Components.utils.cloneInto({foo: 'bar'}, doc.defaultView);
  var myEvent = doc.defaultView.CustomEvent("mytype", eventDetail);
  doc.dispatchEvent(myEvent);
}
									

But one needs to keep in mind that exposing a function will allow the content script to run it with chrome privileges, which can open a security vulnerability.

另请参阅

元数据

  • 最后修改: