在javascript中关闭弹出/模态的逻辑

时间:2017-07-31 10:11:35

标签: javascript

我正在尝试使用以下功能实现两个非常简单的弹出窗口/模式 -  1.单击按钮将打开相应的弹出窗口。  2.一旦弹出窗口处于活动状态,单击屏幕上的任意位置,将关闭弹出窗口。  3.单击“X”将关闭弹出窗口。

我能够实现第1点和第2点中提到的目标。但是,我无法实现第3点。

HTML -

<button id="myBtn1">Open Modal 1</button>
<div id="myModal1" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in first Modal</p>
  </div>
</div>


<button id="myBtn2">Open Modal 2</button>
<div id="myModal2" class="modal">
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in second modal</p>
  </div>
</div>

JS - 显然不正确:

var modal1 = document.getElementById('myModal1');
var btn1 = document.getElementById("myBtn1");

var modal2 = document.getElementById('myModal2');
var btn2 = document.getElementById("myBtn2");

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

btn1.onclick = function() {
    modal1.style.display = "block";
}

btn2.onclick = function() {
    modal2.style.display = "block";
}


// 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";
    }
}

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

请在此处找到我的代码 - https://jsfiddle.net/23bL2w9L/

注意 - 我无法以任何方式更改HTML代码。只应修改Javascript。

提前致谢

4 个答案:

答案 0 :(得分:1)

你几乎拥有它。

您可以像这样获得每个模态的关闭按钮

var span1 = modal1.getElementsByClassName("close")[0];

然后像这样绑定点击按钮

span1.onclick = function() {
  modal1.style.display = "none";
}

https://jsfiddle.net/YannBertrand/23bL2w9L/1/

答案 1 :(得分:1)

您的第一个模态只有Close。我已动态修改您的代码以关闭模态窗口。如果您有 10模态关闭按钮,则无需任何修改即可使用。

更改

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

var span = document.querySelectorAll(".close"); //this will get all `.close` elements

更改,

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

for(var i = 0; i < span.length; i++){
  span[i].onclick = function() {
      //modal1.style.display = "none";
      //console.log(this.closest('.modal'));
      this.closest('.modal').style.display = "none";
  }
}

https://jsfiddle.net/23bL2w9L/5/

演示:

&#13;
&#13;
var modal1 = document.getElementById('myModal1');
var btn1 = document.getElementById("myBtn1");

var modal2 = document.getElementById('myModal2');
var btn2 = document.getElementById("myBtn2");

var span = document.querySelectorAll(".close");

btn1.onclick = function() {
    modal1.style.display = "block";
}

btn2.onclick = function() {
    modal2.style.display = "block";
}


// 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";
    }
}

for(var i = 0; i < span.length; i++){
  span[i].onclick = function() {
    //modal1.style.display = "none";
    console.log(this.closest('.modal'));
    this.closest('.modal').style.display = "none";
  }
}
&#13;
/* The Modal (background) */
.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 {
    background-color: #fefefe;
    margin: 15% auto; /* 15% from the top and centered */
    padding: 20px;
    border: 1px solid #888;
    width: 80%; /* 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;
}
&#13;
<!-- Trigger/Open The Modal -->
<button id="myBtn1">Open Modal 1</button>

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in first Modal</p>
  </div>

</div>




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

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

  <!-- Modal content -->
  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in second modal</p>
  </div>

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

答案 2 :(得分:0)

它适用于 modal1 ,为第二个模态( modal2 )执行此操作,请添加以下内容:

var span2 = document.getElementsByClassName("close")[1];
span2.onclick = function() {
    modal2.style.display = "none";
}

工作小提琴:Here

答案 3 :(得分:0)

当我尝试你的小提琴代码时,modal1的按钮关闭可以工作,但不适用于modal2。 在你的JS代码中,你只为modal1创建动作,你必须为modal2创建一个动作......

var modal1 = document.getElementById('myModal1');
var btn1 = document.getElementById("myBtn1");

var modal2 = document.getElementById('myModal2');
var btn2 = document.getElementById("myBtn2");

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

btn1.onclick = function() {
   modal1.style.display = "block";
}

btn2.onclick = function() {
   modal2.style.display = "block";
}


// 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";
    }
}

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

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