CustomEvent:detail 属性

注意:此功能在Web Workers中可用。

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

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

示例

js
// create custom events
const catFound = new CustomEvent("animalfound", {
  detail: {
    name: "cat",
  },
});
const dogFound = new CustomEvent("animalfound", {
  detail: {
    name: "dog",
  },
});

const element = document.createElement("div"); // create a <div> element

// add an appropriate event listener
element.addEventListener("animalfound", (e) => console.log(e.detail.name));

// dispatch the events
element.dispatchEvent(catFound);
element.dispatchEvent(dogFound);

// "cat" and "dog" logged in the console

规范

规范
DOM 标准
# ref-for-dom-customevent-detail②

浏览器兼容性

BCD 表格仅在启用 JavaScript 的浏览器中加载。

另请参阅