CustomEvent:detail 属性

Baseline Widely available

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

备注: 此特性在 Web Worker 中可用。

CustomEvent 接口的 detail 只读属性返回初始化事件时传递的任何数据。

事件初始化时使用的数据。

示例

js
// 创建自定义事件
const catFound = new CustomEvent("animalfound", {
  detail: {
    name: "cat",
  },
});
const dogFound = new CustomEvent("animalfound", {
  detail: {
    name: "dog",
  },
});

const element = document.createElement("div"); // 创建一个 <div> 元素

// 添加适当的事件监听
element.addEventListener("animalfound", (e) => console.log(e.detail.name));

// 派发事件
element.dispatchEvent(catFound);
element.dispatchEvent(dogFound);

// 在控制台中输出“cat”和“dog”

规范

Specification
DOM
# ref-for-dom-customevent-detail②

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
detail

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见