type 只读特性在 事件 interface returns a string containing the event's type. It is set when the event is constructed and is the name commonly used to refer to the specific event, such as click , load ,或 error .

For a list of available event types, see the event reference .

句法

let eventType = event.type;
					

A DOMString containing the type of 事件 .

范例

This example logs the event type whenever you press a keyboard key or click a mouse button.

HTML

<p>Press any key or click the mouse to get the event type.</p>
<p id="log"></p>
					

JavaScript

function getEventType(event) {
  const log = document.getElementById('log');
  log.innerText = event.type + '\n' + log.innerText;
}
// Keyboard events
document.addEventListener('keydown', getEventType, false);  // first
document.addEventListener('keypress', getEventType, false); // second
document.addEventListener('keyup', getEventType, false);    // third
// Mouse events
document.addEventListener('mousedown', getEventType, false); // first
document.addEventListener('mouseup', getEventType, false);   // second
document.addEventListener('click', getEventType, false);     // third
					

结果

规范

规范 状态 注释
DOM
The definition of 'Event.type' in that specification.
实时标准
DOM (文档对象模型) 2 级事件规范
The definition of 'Event.type' in that specification.
过时 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
type Chrome 1 Edge 12 Firefox 1 IE 9 Opera 7 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改: