簡體   English   中英

如何根據文本有條件地阻止警報?

[英]How to prevent Alerts conditionally based on the text?

我有以下情況,當消息為“ hi”時,需要防止出現警告框。 對於所有其他情況,應顯示警告框。

window.alert = function(text) {
  if(text=='hi') {
    console.log('Prevented alert Box');
  } else {
    // Continue displaying Alert. 
  }
};

我不確定這里是否正確。 任何幫助是極大的贊賞。 提前致謝。

您需要保留對舊警報的引用。

例如。

 var old_alert = window.alert; window.alert = function(text) { if(text=='hi') { console.log('Prevented alert Box'); } else { old_alert(text); } }; alert("hi"); alert("there"); 

通過執行以下操作來保存alert的原始版本:

window.originalAlert = window.alert;

然后像上面一樣重新定義警報,如下所示:

window.alert = function(text) {
  if(text=='hi') {
    console.log('Prevented alert Box');
  } else {
    window.originalAlert(text); 
  }
};

暫無
暫無

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

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