CustomEvent()
构造函数创建新
CustomEvent
.
event = new CustomEvent(typeArg, customEventInit);
typeArg
DOMString
表示事件的名称。
customEventInit
可选
CustomEventInit
字典,拥有以下字段:
"detail"
, optional and defaulting to
null
, of type any, that is an event-dependent value associated with the event.
CustomEventInit
字典还接受字段来自
EventInit
字典。
新的
CustomEvent
object of the specified type, with any other properties configured according to the
CustomEventInit
dictionary (if one was provided).
// add an appropriate event listener
obj.addEventListener("cat", function(e) { process(e.detail) });
// create and dispatch the event
var event = new CustomEvent("cat", {
detail: {
hazcheeseburger: true
}
});
obj.dispatchEvent(event);
Additional examples can be found at 创建和触发事件 .
| 规范 | 状态 | 注释 |
|---|---|---|
|
DOM
The definition of 'CustomEvent()' in that specification. |
实时标准 | 初始定义。 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
You can polyfill the
CustomEvent()
constructor functionality in Internet Explorer 9 and higher with the following code:
(function () {
if ( typeof window.CustomEvent === "function" ) return false;
function CustomEvent ( event, params ) {
params = params || { bubbles: false, cancelable: false, detail: null };
var evt = document.createEvent( 'CustomEvent' );
evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
return evt;
}
window.CustomEvent = CustomEvent;
})();
Internet Explorer >= 9 adds a CustomEvent object to the window, but with correct implementations, this is a function.
CustomEvent
CustomEvent()
AbortController
AbortSignal
AbstractRange
Attr
ByteString
CDATASection
CSSPrimitiveValue
CSSValue
CSSValueList
CharacterData
ChildNode
注释
DOMConfiguration
DOMError
DOMErrorHandler
DOMException
DOMImplementation
DOMImplementationList
DOMImplementationRegistry
DOMImplementationSource
DOMLocator
DOMObject
DOMParser
DOMPoint
DOMPointInit
DOMPointReadOnly
DOMRect
DOMString
DOMTimeStamp
DOMTokenList
DOMUserData
Document
DocumentFragment
DocumentType
元素
ElementTraversal
Entity
EntityReference
事件
EventTarget
HTMLCollection
MutationObserver
节点
NodeFilter
NodeIterator
NodeList
NonDocumentTypeChildNode
ProcessingInstruction
PromiseResolver
范围
StaticRange
文本
TextDecoder
TextEncoder
TimeRanges
TreeWalker
TypeInfo
USVString
UserDataHandler
XMLDocument