CustomEvent
interface 是應用程式為了任意目的所初始化的事件。
建構式
CustomEvent()
- 建立一個
CustomEvent。
屬性
CustomEvent.detail
Read only- 初始化事件時傳送的任意資料。
此介面繼承了其父介面 Event
的屬性:
Event.bubbles
Read only- 布林值,表示事件是否會向上冒泡傳遞。
Event.cancelBubble
- 由於歷史性因素,此屬性的效果等同於
stopPropagation()
方法。若在事件處理器回傳前設定此值為true
,可阻止事件繼續向上冒泡傳遞。 Event.cancelable
Read only- 布林值,表示事件是否能被取消。
Event.composed
Read only- A Boolean value indicating whether or not the event can bubble across the boundary between the shadow DOM and the regular DOM.
Event.currentTarget
Read only- 指向目前處理事件之監聽器所屬的 DOM 物件。
Event.deepPath
- An
Array
of DOMNode
s through which the event has bubbled. Event.defaultPrevented
Read only- 布林值,表示事件的預設行為是否被
preventDefault()
方法所取消。 Event.eventPhase
Read only- 表示事件目前的傳遞階段。
Event.explicitOriginalTarget
Read only- 事件的明確原定目標(Mozilla 專屬)。
Event.originalTarget
Read only- 事件重新導向前的原定目標(Mozilla 專屬)。
Event.returnValue
- 非標準屬性。用以替代
preventDefault()
方法與defaultPrevented
屬性(舊版 Internet Explorer 專屬)。 Event.scoped
Read only- A
Boolean
indicating whether the given event will bubble across through the shadow root into the standard DOM. This property has been renamed tocomposed
. Event.srcElement
- 非標準屬性。為
target
屬性的別名(舊版 Internet Explorer 專屬)。 Event.target
Read only- 指向最初觸發事件的 DOM 物件。
Event.timeStamp
Read only- 事件發生(事件物件建立)至今的時間。
Event.type
Read only- 事件類型(不區分大小寫)。
Event.isTrusted
Read only- 表示事件物件是否為瀏覽器建立(比如在用戶點擊之後),或由程式碼所建立(使用建立事件的方法,如
initEvent()
)。
Obsolete properties
Event.scoped
Read only- A
Boolean
indicating whether the given event will bubble across through the shadow root into the standard DOM. This property has been renamed tocomposed
.
方法
CustomEvent.initCustomEvent()
-
初始化一
CustomEvent
object。若該事件已經被觸發,則不會進行任何動作。
此介面繼承了其父介面 Event
的方法:
Event.createEvent()
-
Creates a new event, which must then be initialized by calling its
initEvent()
method. 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()
- 取消該事件(如果該事件的
cancelable
屬性為true
)。 Event.stopImmediatePropagation()
- 一旦事件物件呼叫此方法,目前元素中尚未執行的已註冊之相同事件類型監聽器將不會被呼叫,而事件也不會繼續捕捉或冒泡傳遞。
Event.stopPropagation()
- 阻止事件物件繼續捕捉或冒泡傳遞。
已廢棄方法
Event.getPreventDefault()
- 非標準方法。回傳
defaultPrevented
屬性值。請直接改用defaultPrevented
屬性。 Event.preventBubble()
已過時 Gecko 24- 已廢棄方法。阻止事件繼續冒泡傳遞。請改用
stopPropagation()
方法。 Event.preventCapture()
已過時 Gecko 24- 已廢棄方法。請改用
stopPropagation()
方法。
規格
Specification | Status | Comment |
---|---|---|
DOM The definition of 'CustomEvent' in that specification. |
Living Standard | 原始定義 |
瀏覽器兼容性
BCD tables only load in the browser
Firing from privileged code to non-privileged code
當要從 privileged code (像是插件)到非 privileged code (例如網頁)執行 CustomEvent ,你必須要考慮這之間的安全性。Firefox 和其他 Gecko 應用會對此有所限制。雖然這可以自動防止安全漏洞發生,但也可能導致您的程式碼沒辦法正常執行。
When creating a CustomEvent object, you must create the object from the same window as you're going to fire against. The detail
attribute of your CustomEvent will be subject to the same restrictions. String and Array values will be readable by the content without restrictions, but custom Objects will not. If 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);
}
Note that exposing a function will allow the content script to run it with chrome privileges, which can open a security vulnerability.