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 |
瀏覽器相容性
載入中…