我已经向w3school求助创建了一个模态框。它工作得很好,但有一个问题。它只适用于1个模态框。假设页面有两个模态框。我叫它modal1和modal2。我想看看是否可以创建一个模式框,而不必为每个模式框编写两次相同的javascript代码。。
HTML和CSS如下:

<!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 {
    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>

<p>this is the section for testing modal box</p>

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


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



<!-- The Modal 2 -->
<div id="myModal2" class="modal">
  <!-- Modal content 2 -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>SECOND Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body 2</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>SECOND Modal Footer</h3>
    </div>
  </div>
</div>

<script>
var modal = document.getElementById('myModal1');

var btn = document.getElementById("myBtn1");

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

那么,如何打开由按钮myBtn2触发的第二个模式框,并打开第二个,而不必创建另一个相同的javascript代码呢?

最佳答案

Working fiddle
由于可以使用jquery,因此可以使用具有data-*属性的常规类:

$('.my-btn').on('click', function(){
  $('#'+$(this).data('modal')).css('display','block');
})

$('.close').on('click', function(){
  $('.modal').hide();
})

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target.className == 'modal') {
    $('.modal').hide();
  }
}

通过给按钮提供相同的类(my-btn在我的示例中),您可以将事件附加到类一次,并将相关的模式id存储在数据属性中(data-modal在我的示例中),然后当单击按钮时,检索id并显示模式。
希望这有帮助。
    $('.my-btn').on('click', function(){
      $('#'+$(this).data('modal')).css('display','block');
    })

    $('.close').on('click', function(){
      $('.modal').hide();
    })

    // When the user clicks anywhere outside of the modal, close it
    window.onclick = function(event) {
      if (event.target.className == 'modal') {
        $('.modal').hide();
      }
    }

.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;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>this is the section for testing modal box</p>

<!-- Trigger/Open The Modal -->
<button class="my-btn" data-modal='myModal1'>Open Modal</button>
<button class="my-btn" data-modal='myModal2'>Open Modal</button>


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



<!-- The Modal 2 -->
<div id="myModal2" class="modal">
  <!-- Modal content 2 -->
  <div class="modal-content">
    <div class="modal-header">
      <span class="close">×</span>
      <h2>SECOND Modal Header</h2>
    </div>
    <div class="modal-body">
      <p>Some text in the Modal Body 2</p>
      <p>Some other text...</p>
    </div>
    <div class="modal-footer">
      <h3>SECOND Modal Footer</h3>
    </div>
  </div>
</div>

关于javascript - 使用两个模态框和一个JavaScript代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40870872/

10-12 12:56