事件:type 屬性
注意:此功能在 Web Workers 中可用。
type 是 Event 介面的一個只讀屬性,它返回一個包含事件型別的字串。該屬性在事件被構造時設定,並且是通常用於指代特定事件的名稱,例如 click、load 或 error。
值
一個包含 Event 事件型別的字串。
示例
此示例在您按下鍵盤按鍵或單擊滑鼠按鈕時會記錄事件型別。
HTML
html
<p>Press any key or click the mouse to get the event type.</p>
<p id="log"></p>
JavaScript
js
function getEventType(event) {
const log = document.getElementById("log");
log.innerText = `${event.type}\n${log.innerText}`;
}
// Keyboard events
document.addEventListener("keydown", getEventType); // first
document.addEventListener("keypress", getEventType); // second
document.addEventListener("keyup", getEventType); // third
// Mouse events
document.addEventListener("mousedown", getEventType); // first
document.addEventListener("mouseup", getEventType); // second
document.addEventListener("click", getEventType); // third
結果
規範
| 規範 |
|---|
| DOM # ref-for-dom-event-type④ |
瀏覽器相容性
載入中…