【问题标题】:Do we have any methods to prevent dialog box from appearing when the same dialog box is already opened?当同一个对话框已经打开时,我们有什么方法可以防止出现对话框?
【发布时间】:2022-01-11 10:56:38
【问题描述】:

我正在尝试修复出现多个对话框的问题,在按下 fn 键时,对话框似乎是从用户那里获取输入。每当我按下 fn 键时,即使已经存在同一个对话框,也会出现对话框打开。然后用户需要多次按取消来关闭此对话框。所以我需要防止这个多重对话框在它已经打开时出现。打开对话框的条件是用Js写的

【问题讨论】:

  • 请分享您在问题中尝试过的代码,以便社区可以更好地帮助您。

标签: javascript dialog appearance


【解决方案1】:

您没有描述您使用的特定对话框,因此我创建了一个示例并演示了解决方案。 你应该怎么做才能实现单例设计模式,这是我在 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>

codepen

【讨论】:

  • 感谢您的解决方案和我的代码如下: function key(g) { if (g.which == 116 ) { dialogbox('Confirm', 802044, undefined, 'BOTH').then( () => location.reload());返回假; } else if (g.keyCode == 82 && g.ctrlKey && !g.altKey ) { return false; }}
  • 这里是考虑到您的示例代码的示例代码。请注意,我必须模拟您的 dialogBox 函数。此外,您必须为关闭事件提供 closeModal 方法。 codepen.io/farshad-kazemi/pen/jOGrJyq?editors=1111
猜你喜欢
  • 1970-01-01
  • 2011-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 2015-12-30
  • 1970-01-01
相关资源
最近更新 更多