繁体   English   中英

使用两个模态

[英]Using two modals

我确定这是一个非常简单的问题,但我无法使用javascript打开两个模态窗口。

期望的效果是每个按钮打开自己的模态

JS FIDDLE

HTML

<!-- Modal 1 -->
<div id="myModal-1" class="modal">
  <div class="modal-content">
    <div class="box-content">
      <h2>Modal 1</h2>
      <span class="close">&times;</span>
    </div>
  </div>
</div>

<!-- Modal 2 -->
<div id="myModal-2" class="modal">
  <div class="modal-content">
    <div class="box-content">
      <h2>Modal 2</h2>
      <span class="close">&times;</span>
    </div>
  </div>
</div>

<button id="modalBtn-1">Modal 1</button>
<button id="modalBtn-2">Modal 2</button>

JS

// Get the modal 1
var modal = document.getElementById('myModal-1');

var btn = document.getElementById("modalBtn-1");

var span = document.getElementsByClassName("close")[0];

btn.onclick = function() {
    modal.style.display = "block";
}

span.onclick = function() {
    modal.style.display = "none";
}

window.onclick = function(event) {
    if (event.target == modal) {
        modal.style.display = "none";
    }
}

您遇到的问题是第二个功能是覆盖第一个事件的事件。

如果您必须执行2个以上的模态,您可能需要考虑修改函数以使其更具通用性

 // Get the modal var modal = document.getElementById('myModal-1'); // Get the button that opens the modal var btn = document.getElementById("modalBtn-1"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks on the button, open the modal btn.onclick = function() { modal.style.display = "block"; } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } // Get the modal var modal = document.getElementById('myModal-2'); // Get the button that opens the modal var btn = document.getElementById("modalBtn-2"); // Get the <span> element that closes the modal var span = document.getElementsByClassName("close")[0]; // When the user clicks on the button, open the modal btn.onclick = function() { modal.style.display = "block"; } // When the user clicks on <span> (x), close the modal span.onclick = function() { modal.style.display = "none"; } // When the user clicks anywhere outside of the modal, close it window.onclick = function(event) { if (event.target == modal) { modal.style.display = "none"; } } 
 .modal { display: none; /* Hidden by default */ position: fixed; /* Stay in place */ z-index: 1; /* Sit on top */ left: 0; top: 0; width: 100%; /* Full width */ height: 100%; /* Full height */ overflow: auto; /* Enable scroll if needed */ background-color: rgb(0,0,0); /* Fallback color */ background-color: rgba(0,0,0,0.4); /* Black w/ opacity */ } /* Modal Content/Box */ .modal-content { margin: 10% auto; /* 15% from the top and centered */ max-width: 800px; /* Could be more or less, depending on screen size */ } /* The Close Button */ .close { color: #aaa; float: right; font-size: 28px; font-weight: bold; } .close:hover, .close:focus { color: black; text-decoration: none; cursor: pointer; } .box-content { background: #fff; margin: 0px 20px; height: 100px; } h2 { float: left; } 
 <!-- Modal 1 --> <div id="myModal-1" class="modal"> <div class="modal-content"> <div class="box-content"> <h2>Modal 1</h2> <span class="close">&times;</span> </div> </div> </div> <!-- Modal 2 --> <div id="myModal-2" class="modal"> <div class="modal-content"> <div class="box-content"> <h2>Modal 2</h2> <span class="close">&times;</span> </div> </div> </div> <button id="modalBtn-1">Modal 1</button> <button id="modalBtn-2">Modal 2</button> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM