簡體   English   中英

當同一個對話框已經打開時,我們有什么方法可以防止出現對話框?

[英]Do we have any methods to prevent dialog box from appearing when the same dialog box is already opened?

我正在嘗試修復出現多個對話框的問題,在按下 fn 鍵時,對話框似乎從用戶那里獲取輸入。每當我按下 fn 鍵時,即使已經打開了同一個對話框,也會出現對話框。然后用戶需要多次按取消才能關閉此對話框。 所以我需要防止這個多重對話框在它已經打開時出現。打開對話框的條件是用Js寫的

您沒有描述您使用的特定對話框,因此我創建了一個示例並演示了解決方案。 你應該怎么做才能實現 singleton 設計模式,這是我在 codepen 中編寫的示例代碼。 JS:

class singleModal{
  static isOpen=false;
  static id='dialog';
  static openModal(){    
    if(!singleModal.isOpen) {
      console.log("modal opened!")
      singleModal.isOpen = true;
      document.getElementById(singleModal.id).style.display='block';
    }
  }
  static closeModal(){    
    if(singleModal.isOpen) {
      console.log("modal closed!")
      singleModal.isOpen = false;
      document.getElementById(singleModal.id).style.display='none';
    }
  }
}


 singleModal.id = 'myDialog';
function openModal(){
singleModal.openModal();
}
function closeModal(){
singleModal.closeModal();
}
 

HTML:

<div class="dialog" id="myDialog">
  Dialog
</div>

<button onclick="openModal()">open</button>
<button onclick="closeModal()">close</button>

密碼筆

暫無
暫無

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

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