当用户在同一页面上有多个模态时单击模态外的任何位置时,如何关闭模态

时间:2017-05-24 08:51:37

标签: javascript jquery html css twitter-bootstrap

我正在使用W3 Schools中的模态示例  对于同一页面上的两个按钮,每个按钮打开不同的模态。

问题是当用户点击它以外时,控制模态关闭的功能仅适用于一个模态但不适用于第二个模态。

我尝试了很多选项,但似乎没有其他选项。

我尝试为 if 条件添加 OR 语句,但没有任何结果:

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

我也尝试为每个模态编写单独的函数:

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

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

你知道我在哪里错过了这一点吗?

非常感谢所有人的帮助,期待。

2 个答案:

答案 0 :(得分:0)

Node.contains可以实现您的目标,此处摘自MDN

  

Node.contains()方法返回一个布尔值,指示是否   节点是否是给定节点的后代。

     

语法node.contains(otherNode)

我附上了一个例子来说明它在实践中是如何运作的



// Get the modal
var modal1 = document.getElementById('modal-1');
var modal2 = document.getElementById('modal-2');

// Get the button that opens the modal
var btn1 = document.getElementById("modal-1-button");
var btn2 = document.getElementById("modal-2-button");


// When the user clicks the button, open the modal 
btn1.onclick = function(event) {
    event.preventDefault();
    event.stopPropagation();
    modal1.style.display = "block";
}
btn2.onclick = function(event) {
    event.preventDefault();
    event.stopPropagation();
    modal2.style.display = "block";
}



// When the user clicks anywhere outside of the modal, close it

window.onclick = function (event) {
  var modal1 = document.getElementById("modal-1");
  var modal2 = document.getElementById("modal-2");
  if (modal1.style.display === "block") {
    var modal1Content = modal1.querySelector(".modal-content");
    if (!modal1Content.contains(event.target)) {
      modal1.style.display = "none";
    }    
  }
  if (modal2.style.display === "block") {
    var modal2Content = modal1.querySelector(".modal-content");
    if (!modal2Content.contains(event.target)) {
      modal2.style.display = "none";
    }    
  }
};

#modal-1 {
  
  background-color: green;
}

#modal-2 {
 
  background-color: yellow;
}

<html>
<head>
<style>
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    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 */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
</style>
</head>
<body>

<h2>Animated Modal with Header and Footer</h2>

<!-- Trigger/Open The Modal -->
<button id="modal-1-button">Modal - 1</button>
<button id="modal-2-button">Modal - 2</button>

<!-- The Modal -->
<div id="modal-1" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">     
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>

<div id="modal-2" class="modal">

  <!-- Modal content -->
  <div class="modal-content">
    <div class="modal-header">     
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>

</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

这可能很简单,但它适用于我的情况。

&#13;
&#13;
// 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 span1 = document.getElementsByClassName("close")[0];
var span2 = document.getElementsByClassName("close second")[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
span1.onclick = function() {
    modal1.style.display = "none";
}

span2.onclick = function() {
    modal2.style.display = "none";
}

window.onclick = function(event) {
    if (event.target == modal1) {
        modal1.style.display = "none";
    }
    if (event.target == modal2) {
        modal2.style.display = "none";
    }
}
&#13;
/* The Modal (background) */
.modal {
    display: none; /* Hidden by default */
    position: fixed; /* Stay in place */
    z-index: 1; /* Sit on top */
    padding-top: 100px; /* Location of the box */
    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 */
.modal-content {
    position: relative;
    background-color: #fefefe;
    margin: auto;
    padding: 0;
    border: 1px solid #888;
    width: 80%;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2),0 6px 20px 0 rgba(0,0,0,0.19);
    -webkit-animation-name: animatetop;
    -webkit-animation-duration: 0.4s;
    animation-name: animatetop;
    animation-duration: 0.4s
}

/* Add Animation */
@-webkit-keyframes animatetop {
    from {top:-300px; opacity:0} 
    to {top:0; opacity:1}
}

@keyframes animatetop {
    from {top:-300px; opacity:0}
    to {top:0; opacity:1}
}

/* The Close Button */
.close {
    color: white;
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}

.modal-header {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}

.modal-body {padding: 2px 16px;}

.modal-footer {
    padding: 2px 16px;
    background-color: #5cb85c;
    color: white;
}
&#13;
  <button id="myBtn1" class="btn" type="button">Modal 1</button>  
  <button id="myBtn2" class="btn" type="button">Modal 2</button>

  <!-- The Modal -->
  <div id="myModal1" class="modal">
    <!-- Modal content -->
    <div class="modal-content">
      <span class="close">&times;</span>
    <div class="modal-header">
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>
  </div>
</div>

<!-- The Modal -->
<div id="myModal2" class="modal">
<!-- Modal content -->
  <div class="modal-content">
      <span class="close second">&times;</span>
    <div class="modal-header">
      <h2>Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>Modal Footer</h3>
    </div>
  </div>
  </div>
</div>
&#13;
&#13;
&#13;