Notification: click 事件
注意:此功能在 Web Workers 中可用。
当用户点击显示的 Notification 时,Notification 接口的 click 事件会触发。
默认行为是将焦点移至通知相关 浏览上下文 的视口。如果您不希望出现此行为,请在事件对象上调用 preventDefault()。
语法
在诸如 addEventListener() 之类的方法中使用事件名称,或设置事件处理程序属性。
js
addEventListener("click", (event) => { })
onclick = (event) => { }
事件类型
一个通用的 Event。
示例
在以下示例中,我们使用 onclick 处理程序,在用户点击通知后,会在新选项卡中打开一个网页(通过包含 '_blank' 参数指定)。
js
notification.onclick = (event) => {
event.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open("https://www.mozilla.org", "_blank");
};
规范
| 规范 |
|---|
| Notifications API # dom-notification-onclick |
浏览器兼容性
加载中…