Movatterモバイル変換


[0]ホーム

URL:


  1. 給開發者的 Web 技術文件
  2. Web API
  3. Event

此頁面由社群從英文翻譯而來。了解更多並加入 MDN Web Docs 社群。

View in EnglishAlways switch to English

Event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2015年7月.

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

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

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

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

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 theEvent bubbling and capture settings of each handler triggered.

基於Event 的子介面

Below is a list of interfaces which are based on the mainEvent interface, with links to their respective documentation in the MDN API reference. Note that all event interfaces have names which end in "Event".

建構式

Event()

建立一個Event 物件。

屬性

Event.bubblesRead only

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

Event.cancelBubble

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

Event.cancelableRead only

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

Event.composedRead only

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

Event.currentTargetRead only

指向目前處理事件之監聽器所屬的 DOM 物件。

Event.deepPathNon-standard

AnArray of DOMNodes through which the event has bubbled.

Event.defaultPreventedRead only

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

Event.eventPhaseRead only

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

Event.explicitOriginalTargetNon-standardRead only

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

Event.originalTargetNon-standardRead only

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

Event.returnValue

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

Event.scopedRead only已棄用

ABoolean indicating whether the given event will bubble across through the shadow root into the standard DOM. This property has been renamed tocomposed.

Event.srcElementNon-standard

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

Event.targetRead only

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

Event.timeStampRead only

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

Event.typeRead only

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

Event.isTrustedRead only

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

Obsolete properties

Event.scopedRead only已棄用

ABoolean indicating whether the given event will bubble across through the shadow root into the standard DOM. This property has been renamed tocomposed.

方法

Event.createEvent()已棄用

Creates a new event, which must then be initialized by calling itsinitEvent() 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 itsShadowRoot.mode closed.

Event.initEvent()已棄用

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

Event.preventDefault()

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

Event.stopImmediatePropagation()

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

Event.stopPropagation()

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

已廢棄方法

Event.getPreventDefault()Non-standard

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

Event.preventBubble()Non-standard已棄用

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

Event.preventCapture()Non-standard已棄用

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

規範

Specification
DOM
# interface-event

瀏覽器相容性

參見

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp