簡體   English   中英

如何將警報與onclick事件結合

[英]How to combine alert with onclick event

目前,我有一個警報框,一段時間后會彈出。 當我單擊“確定”時,我想將該click事件分配給console.log之類的操作以記錄某些內容。 我已經嘗試過“ alert.onclick”,但這似乎不起作用。 任何幫助/建議將不勝感激! :)

您可以覆蓋window.alert

看看這個代碼片段

當然,即使您按Esc鍵,也會調用該日志。

 var nativeAlert = window.alert; window.alert = function(msg) { nativeAlert(msg); console.log('After OK!! :-)'); }; alert('Hello World!'); 

如果您不覆蓋alert(),則不會返回任何內容。 相反,您可以使用confirm

  // result is a boolean variable var result = confirm( "Do you want to continue" ); if ( result ) { // ok is clicked console.log("ok"); } else { // the user clicked cancel or closed the confirm dialog. } 

我發現的另一個智能解決方案是

 function function_to_call_when_oked_or_closed () { console.log("closed"); } alert('foo'); function_to_call_when_oked_or_closed(); 

這是可能的,因為alert()是同步的(如果不關閉它,則下面的代碼將不會執行。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM