Event

Event 介面表示了一個在 DOM 物件上所發生的事件。

一個事件可以是由使用者的操作行為所產生(如:點擊滑鼠按鈕或敲打鍵盤),或是來自 API 因處理非同步任務所產生。事件也可以為程式所觸發,例如呼叫元素的 HTMLElement.click() 方法,或是自行定義事件並使用 EventTarget.dispatchEvent() 發送至特定的目標。

事件有多種不同的類型,部分事件是使用基於 Event 的子介面。Event 本身包含了所有事件共同的屬性及方法。

許多 DOM 元素可被設定接受(accept,或稱為 listen "監聽")這些事件,並在發生時執行處理(process、handle)事件的程式碼。事件處理器(Event-handlers)通常會使用 EventTarget.addEventListener() 來被連結(connected,或稱為 attached "附加")至各個 HTML 元素 (en-US)(例如 <button>、<div>、<div>、<span> 等),且此方式一般也是用來取代舊的 HTML 事件處理器屬性(attributes) (en-US)。此外,在需要時也可以使用 removeEventListener() (en-US) 來中斷事件處理器與元素的連結。請留意一個元素可以擁有多個事件處理器(即使是處理同一種事件的不同處理器),特別是那些被切分開來彼此獨立且有不同目標的程式模組(舉例來說,廣告及統計模組可以同時監控網頁中的影片觀看資訊)。

When there are many nested elements, each with its own handler(s), event processing can become very complicated — especially where a parent element receives the very same event as its child elements because "spatially" they overlap so the event technically occurs in both, and the processing order of such events depends on the Event bubbling and capture (en-US) settings of each handler triggered.

基於 Event 的子介面

建構式

Event()

建立一個 Event 物件。

屬性

Event.bubbles Read only

布林值,表示事件是否會向上冒泡傳遞。

Event.cancelBubble (en-US)

由於歷史性因素,此屬性的效果等同於 stopPropagation() 方法。若在事件處理器回傳前設定此值為 true,可阻止事件繼續向上冒泡傳遞。

Event.cancelable (en-US) Read only

布林值,表示事件是否能被取消。

Event.composed (en-US) 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 (en-US) 非標準

An Array of DOM Node (en-US)s through which the event has bubbled.

Event.defaultPrevented Read only

布林值,表示事件的預設行為是否被 preventDefault() 方法所取消。

Event.eventPhase Read only

表示事件目前的傳遞階段。

Event.explicitOriginalTarget (en-US) 非標準 Read only

事件的明確原定目標(Mozilla 專屬)。

Event.originalTarget (en-US) 非標準 Read only

事件重新導向前的原定目標(Mozilla 專屬)。

Event.returnValue (en-US)

非標準屬性。用以替代 preventDefault() 方法與 defaultPrevented 屬性(舊版 Internet Explorer 專屬)。

Event.scoped (en-US) 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 to composed (en-US).

Event.srcElement (en-US) 非標準

非標準屬性。為 target 屬性的別名(舊版 Internet Explorer 專屬)。

Event.target Read only

指向最初觸發事件的 DOM 物件。

Event.timeStamp Read only

事件發生(事件物件建立)至今的時間。

Event.type Read only

事件類型(不區分大小寫)。

Event.isTrusted Read only

表示事件物件是否為瀏覽器建立(比如在用戶點擊之後),或由程式碼所建立(使用建立事件的方法,如 initEvent() (en-US))。

Obsolete properties

Event.scoped (en-US) 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 to composed (en-US).

方法

Event.createEvent() (en-US) 已棄用

Creates a new event, which must then be initialized by calling its initEvent() method.

Event.composedPath() (en-US)

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 (en-US) closed.

Event.initEvent() (en-US) 已棄用

初始化已經建立的事件。若該事件已經被處理過,這方法就不會執行任何東西。

Event.preventDefault()

取消該事件(如果該事件的 cancelable (en-US) 屬性為 true)。

Event.stopImmediatePropagation()

一旦事件物件呼叫此方法,目前元素中尚未執行的已註冊之相同事件類型監聽器將不會被呼叫,而事件也不會繼續捕捉或冒泡傳遞。

Event.stopPropagation()

阻止事件物件繼續捕捉或冒泡傳遞。

已廢棄方法

Event.getPreventDefault() 非標準

非標準方法。回傳 defaultPrevented 屬性值。請直接改用 defaultPrevented 屬性。

Event.preventBubble() 非標準 已棄用

已廢棄方法。阻止事件繼續冒泡傳遞。請改用 stopPropagation() 方法。

Event.preventCapture() 非標準 已棄用

已廢棄方法。請改用 stopPropagation() 方法。

規範

Specification
DOM Standard
# interface-event

瀏覽器相容性

BCD tables only load in the browser

參見