jquery 多个div的PrePended按钮无法打开所有div的popin

bttbmeg0  于 2022-12-18  发布在  jQuery
关注(0)|答案(1)|浏览(151)

我有一个触发popin的按钮。我把它附加到一个div上,然后把这个div附加到同一个类的所有div上。
我的问题是只有最后一个按钮会触发popin,其他按钮似乎不起作用。
有人能帮帮我吗?
JSFiddle Here

// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
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";
  }
}

$(window).on('load', function() {
  function topBannerCaps() {
    if ($('#container').css('display') == 'block') {
    
      $('#myBtn').appendTo('#container');
    }
    $('#container').prependTo('.test-div');
  }
  requestAnimationFrame(topBannerCaps);
});
n9vozmp4

n9vozmp41#

你的代码有几个问题。

  1. ID必须唯一,因此在追加时使用按钮上的class而不是id
    1.不要让JavaScript和jQuery不匹配。
    1.尽可能避免内联类。
    工作小提琴:
// Get the modal
var modal = $("#myModal");

// Get the button that opens the modal
var btn = $(".myBtn");

// Get the <span> element that closes the modal
var span = $(".close");

// When the user clicks the button, open the modal 
btn.on("click", () => {
  $(modal).css("display", "block")
});

// When the user clicks on <span> (x), close the modal
span.on("click", () => {
  $(modal).css("display", "none")
});

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    $(modal).css("display", "none")
  }
}

$(window).on('load', function() {
  function topBannerCaps() {
    if ($('#container').css('display') == 'block') {
      btn.appendTo('#container');
    }
    $('#container').prependTo('.test-div');
  }
  requestAnimationFrame(topBannerCaps);
});
.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;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!-- Trigger/Open The Modal -->
<button class="myBtn">Open Modal</button>

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

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

</div>

<div id="container" style="display: block"></div>

<div class="example">
  <div class="test-div">Example DIV</div>
  <div class="test-div">Example DIV</div>
  <div class="test-div">Example DIV</div>
  <div class="test-div">Example DIV</div>
  <div class="test-div">Example DIV</div>
  <div class="test-div">Example DIV</div>
</div>

相关问题