如何在具有相同 class 的多个按钮的点击事件中添加多个具有 Javascript 的相同 ID

How to add muitple same ID with Javascript on click event for multiple buttons with same class

我是 JQuery 的新手。我正在使用 Silverstripe PHP。我已经设法获得具有多个具有相同 classes 的 div 的点击事件,但是如果我想在具有相同 class 和相同 ID 的第二个按钮中有不同的图像。这个问题的解决方案是什么?

我知道它必须是不同的 ID 才能工作,但是每个按钮作为循环功能所以它必须是相同的 ID。

file.ss:

<div class="PosterButton">
   <button class="posterBtn button" id="myBtn" value="$ClassPosterID">
       <i class="fas fa-image"></i> View poster
   </button>
</div>
<div class="myModal modal" id="myModal">
   <div class="modal-content">
       <span class="close">&times;</span>
       <img src="image-1.jpg" class="modal-content" />
   </div>
</div>

<div class="PosterButton">
   <button class="posterBtn button" id="myBtn" value="$ClassPosterID">
       <i class="fas fa-image"></i> View poster
   </button>
</div>
<div class="myModal modal" id="myModal">
   <div class="modal-content">
       <span class="close">&times;</span>
       <img src="image-2.jpg" class="modal-content" />
   </div>
</div>

main.css:

.modal {
   display: none;
   position: fixed;
   z-index:9999;
   padding-top:60px;
   left: 0;
   top: 0;
   width: 100%;
   height: 100%;
   overflow: auto;
   background-color: rgb(0,0,0);
   background-color: rgba(0,0,0,0.4);
}
.modal-content {
   margin: auto;
   display: block;
   width: 80%;
   max-width: 700px;
}
.modal-content {
   -webkit-animation-name: zoom;
   -webkit-animation-duration: 0.6s;
   animation-name: zoom;
   animation-duration: 0.6s;
}

jQuery.js:

    var modal = document.getElementById('myModal');
    var span = document.getElementsByClassName("close")[0];
    $(document).ready(function() {
        $('.posterBtn').click(function(event) {
        event.preventDefault();
            modal.style.display = "block";
            });
    });
    span.onclick = function() {
      modal.style.display = "none";
    }
    window.onclick = function(event) {
      if (event.target == modal) {
        modal.style.display = "none";
      }
    }

我点击的第一个按钮在弹出第一张图片时工作正常,但当我点击第二个按钮时,它工作正常但弹出与第一张图片相同的第一个 ID。它应该显示第二个 ID 和第二个图像。

实现此功能的正确方法是什么?我做错了吗?

由于您使用的是 JQuery,因此您可以轻松创建一个 select 来指向您的第二个 PosterButton,而无需处理 ID。

  • 如果您决定使用 jquery,那么请使用 jquery 不要将纯 javascript 与 jquery
  • 结合使用
  • ID 应该是唯一的,所以即使它来自循环,你也可以添加 id="myModal+' i '+" 来向 id 添加一个数字。如果 php 中的循环使用 id="myModal.' i '."

  • 问题是你通过 id 定义 myModal 并且每次都会捕获 same/first 模态..你需要将模态引用到你点击的按钮


$(document).ready(function() {
   $('.posterBtn').click(function(event) {
       //event.preventDefault();
       var GetModal = $(this).closest('.PosterButton').next('.modal'); // get the next modal
       GetModal.show();  // show the next modal
   });
   $('.modal span.close').on('click', function(){  // modal close span
      $('.modal').hide(); // hide modal
   });

   // To close modal
   // when modal click close the modal
   $('.modal').on('click' , function(){
     $(this).hide();
   });
   // prevent the modal to close when clicking on the modal content div
   $('.modal-content').on('click' , function(e){
    e.stopPropagation();
   });
});

$(document).ready(function() {
   $('.posterBtn').click(function(event) {
       //event.preventDefault();
       var GetModal = $(this).closest('.PosterButton').next('.modal'); // get the next modal
       GetModal.show();  // show the next modal
   });
   $('.modal span.close').on('click', function(){  // modal close span
      $('.modal').hide(); // hide modal
   });
   
   // To close modal
   // when modal click close the modal
   $('.modal').on('click' , function(){
     $(this).hide();
   });
   // prevent the modal to close when clicking on the modal content div
   $('.modal-content').on('click' , function(e){
    e.stopPropagation();
   });
});
.modal {
   display: none;
   position: fixed;
   z-index:9999;
   padding-top:60px;
   left: 0;
   top: 0;
   width: 100%;
   height: 100%;
   overflow: auto;
   background-color: rgb(0,0,0);
   background-color: rgba(0,0,0,0.4);
}
.modal-content {
   margin: auto;
   display: block;
   width: 80%;
   max-width: 700px;
   background : #fff;
}
.modal-content {
   -webkit-animation-name: zoom;
   -webkit-animation-duration: 0.6s;
   animation-name: zoom;
   animation-duration: 0.6s;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="PosterButton">
   <button class="posterBtn button" id="myBtn" value="$ClassPosterID">
       <i class="fas fa-image"></i> View poster 1
   </button>
</div>
<div class="myModal modal" id="myModal-1">
   <div class="modal-content">
       <span class="close">&times;</span>
       <div>Poster 1</div>
       <img src="image-1.jpg" class="modal-content" />
   </div>
</div>

<div class="PosterButton">
   <button class="posterBtn button" id="myBtn" value="$ClassPosterID">
       <i class="fas fa-image"></i> View poster 2
   </button>
</div>
<div class="myModal modal" id="myModal-2">
   <div class="modal-content">
       <span class="close">&times;</span>
       <div>Poster 2</div>
       <img src="image-2.jpg" class="modal-content" />
   </div>
</div>

首先一个ID在页面上应该是唯一的。

至于模态,如果你有一个接一个的触发器和模态,最简单的方法是使用 next() ,它将获得紧随其后的兄弟。

要知道已单击的确切按钮(收到事件操作),我们将使用 this。 在伪代码中,当我们在 this 按钮上 click 找到 next 元素并 show 时。

我根据您的代码在下面做了一个工作示例:

https://jsfiddle.net/ahentea/8vjten5L/1/