javascript - 使用一个 javascript 代码使用两个模态框

标签 javascript jquery html css

我已经引用了 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/

相关文章:

php - JSON 下拉列表未正确填充

javascript - 如何从 Django 中的模态表单更新单个字段?

javascript - 无法让 jQuery 循环插件工作 为什么?

html - css 在小窗口宽度下更改表中元素的顺序

javascript - Web 组件 :host:hover

javascript - 如何使用 jquery javascript 重置整个表单..?

javascript - Webpack:html-loader 无法解析 srcset 图像

jquery - 如何获取第 n 个 jQuery 元素

javascript - 在这种情况下如何使用正则表达式?

html - 为什么父无序列表项内的子无序列表不占用其父元素的整个可用空间?