在灰色区域中按下时,HTML模态不会关闭

时间:2017-08-20 18:54:28

标签: html

我在HTML上创建了2个按钮,按下按钮,它将打开一个模态屏幕。这个eaxmple有两个按钮,但我的目标是一个页面可以有多个按钮。 按 X 时,它会关闭模态屏幕。 但是,如果你按压模态区域外,它也应该关闭它。 这对两个按钮都不起作用。

任何帮助都会受到赞赏,或者如果您知道更简单/更好的方式来使用模态屏幕。 : - )

{

<!DOCTYPE html>
<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 {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

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

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
</style>
</head>
<body>

<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn0">Open Modal 0</button>

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

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

</div>

<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 the Modal 1..</p>
  </div>

</div>

<script>
// Get the modal
var modal0 = document.getElementById('myModal0');
var modal1 = document.getElementById('myModal1');

// Get the button that opens the modal
var btn0 = document.getElementById("myBtn0");
var btn1 = document.getElementById("myBtn1");

// Get the <span> element that closes the modal
var span0 = document.getElementsByClassName("close")[0];
var span1 = document.getElementsByClassName("close")[1];

// When the user clicks the button, open the modal 
btn0.onclick = function() {
    modal0.style.display = "block";
}
// When the user clicks the button, open the modal 
btn1.onclick = function() {
    modal1.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span0.onclick = function() {
    modal0.style.display = "none";
}
span1.onclick = function() {
    modal1.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal0) {
        modal0.style.display = "none";
    }
}
window.onclick = function(event) {
    if (event.target == modal1) {
        modal1.style.display = "none";
    }
}
</script>

</body>
</html>

}

1 个答案:

答案 0 :(得分:0)

你有两个window.onclick = function(event)第二个覆盖第一个。

所以你真的应该只有一个window.onclick处理程序:

<!DOCTYPE html>
<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 {
    background-color: #fefefe;
    margin: auto;
    padding: 20px;
    border: 1px solid #888;
    width: 80%;
}

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

.close:hover,
.close:focus {
    color: #000;
    text-decoration: none;
    cursor: pointer;
}
</style>
</head>
<body>

<h2>Modal Example</h2>

<!-- Trigger/Open The Modal -->
<button id="myBtn0">Open Modal 0</button>

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

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

</div>

<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 the Modal 1..</p>
  </div>

</div>

<script>
// Get the modal
var modal0 = document.getElementById('myModal0');
var modal1 = document.getElementById('myModal1');

// Get the button that opens the modal
var btn0 = document.getElementById("myBtn0");
var btn1 = document.getElementById("myBtn1");

// Get the <span> element that closes the modal
var span0 = document.getElementsByClassName("close")[0];
var span1 = document.getElementsByClassName("close")[1];

// When the user clicks the button, open the modal 
btn0.onclick = function() {
    modal0.style.display = "block";
}
// When the user clicks the button, open the modal 
btn1.onclick = function() {
    modal1.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span0.onclick = function() {
    modal0.style.display = "none";
}
span1.onclick = function() {
    modal1.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
    if (event.target == modal0) {
        modal0.style.display = "none";
    }
    else if (event.target == modal1) {
        modal1.style.display = "none";
    }

}
</script>

</body>
</html>