无法使用多个模态弹出窗口

时间:2017-04-26 03:23:19

标签: javascript html popup modal-dialog

// 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">&times;</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">&times;</span>
    <div><img src="" id="pic" />
      <p id="title"></p>
    </div>
  </div>
</div>

我想使用多个模态弹出窗口,但是当我编写代码时,当我点击它时,第二个按钮无法工作。我在https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal

上使用代码库

1 个答案:

答案 0 :(得分:0)

尝试这样的事情,虽然这不会很完美:

// 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">&times;</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">&times;</span>
    <div><img src="" id="pic" />
      <p id="title"></p>
    </div>
  </div>
</div>