[英]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.