不能使用多个模式弹出窗口 [英] Can't use multiple modal popup
本文介绍了不能使用多个模式弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks 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";
}
}
<button id="myBtn">1</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div><img src="http://green-furniture.com.tw/cdn/cache_products/%e5%9c%b0%e4%b8%ad%e6%b5%b7-sm.png" id="pic" />
<p id="title">ocean</p>
</div>
</div>
</div>
<button id="myBtn">2</button>
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div><img src="" id="pic" />
<p id="title"></p>
</div>
</div>
</div>
我想使用多个模式弹出窗口,但是当我编写代码时,单击第二个按钮将不起作用.我在 https://www.w3schools.com/howto/tryit.asp?filename = tryhow_css_modal
I want to use multiple modal popup, but when I write the code the second button can't work when I click on it. I use the code base on https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal
推荐答案
尝试类似的方法,尽管这样做并不完美:
Try something like this, although this will not work perfectly:
// Get the modal
var modal1 = document.getElementById('myModal1');
var modal2 = document.getElementById('myModal2');
// Get the button that opens the modal
var btn1 = document.getElementById("myBtn1");
var btn2 = document.getElementById("myBtn2");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn1.onclick = function() {
modal1.style.display = "block";
}
btn2.onclick = function() {
modal2.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal1.style.display = "none";
modal2.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal1) {
modal1.style.display = "none";
}
if (event.target == modal2) {
modal2.style.display = "none";
}
}
#myModal1,
#myModal2 {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="myBtn1">1</button>
<div id="myModal1" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div><img src="http://green-furniture.com.tw/cdn/cache_products/%e5%9c%b0%e4%b8%ad%e6%b5%b7-sm.png" id="pic" />
<p id="title">ocean</p>
</div>
</div>
</div>
<button id="myBtn2">2</button>
<div id="myModal2" class="modal">
<div class="modal-content">
<span class="close">×</span>
<div><img src="" id="pic" />
<p id="title"></p>
</div>
</div>
</div>
这篇关于不能使用多个模式弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文