Notification:click 事件

安全上下文: 此项功能仅在一些支持的浏览器安全上下文(HTTPS)中可用。

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

Notification 接口的 click 事件在用户点击显示的 Notification 时触发。

默认的事件行为是将焦点移动到与通知相关浏览上下文的视口上。如果你不想要默认行为,请在事件对象上调用 preventDefault() 方法。

语法

在类似 addEventListener() 这样的方法中使用事件名称,或者设置事件处理器属性。

js
addEventListener("click", (event) => {});

onclick = (event) => {};

事件类型

一个通用的 Event

示例

在下面这个例子中,我们使用 onclick 处理器来监听点击通知的事件,并在新标签页(通过包含一个参数 '_blank')打开一个新标签页:

js
notification.onclick = (event) => {
  event.preventDefault(); // 阻止浏览器聚焦于 Notification 的标签页
  window.open("http://www.mozilla.org", "_blank");
};

规范

Specification
Notifications API Standard
# dom-notification-onclick

浏览器兼容性

BCD tables only load in the browser

参见