如何使用 javascript 中的循环简化此模态代码

How to simplify this modal code with loop in javascript

我试图在打开图像时打开模式。这里的代码可以正常工作,但我想找到一种方法来简化它或缩短代码,以防将来打开更多带有模态的图像。

我在想是否可以使用循环来简化这个问题,但我对 Javascript 还很陌生,还有很多东西要学,这就是为什么我需要这方面的帮助。

<h2>Travel Blog</h2>
<div class="blogThumb">
<!--Thumbnails for blog-->
     <div class="blogformat">
      <img class="blogPhoto" id="myImg1" src="/img/thumb1.jpg"  alt="Italy">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg2" src="/img/thumb2.jpg"  alt="Venice">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg3" src="/img/thumb3.jpg"  alt="Philippines">
     </div>

<!-- The Modal-->
     <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" id="img01">
      <div id="caption"></div>
     </div>


     <script>
     // Get the modal
     var modal = document.getElementById("myModal");
                          
     // Get the image and insert it inside the modal - use its "alt" text as a caption
     var img = document.getElementById("myImg1");
     var img2 = document.getElementById("myImg2");
     var img3 = document.getElementById("myImg3");
     var modalImg = document.getElementById("img01");
     var captionText = document.getElementById("caption");
     img.onclick = function(){
           modal.style.display = "block";
           modalImg.src = this.src;
           captionText.innerHTML = this.alt;
           }

     img2.onclick = function(){
           modal.style.display = "block";
           modalImg.src = this.src;
           captionText.innerHTML = this.alt;
           }

     img3.onclick = function(){
           modal.style.display = "block";
           modalImg.src = this.src;
           captionText.innerHTML = this.alt;
           }


     // Get the <span> element that closes the modal
     var span = document.getElementsByClassName("close")[0];
                          
     // When the user clicks on <span> (x), close the modal
     span.onclick = function() { 
             modal.style.display = "none";
             }
     </script>
                          
</div>

Get all the images, add an event listener and the set whatever needed with the target element - which is clicked one

<h2>Travel Blog</h2>
<div class="blogThumb">
<!--Thumbnails for blog-->
     <div class="blogformat">
      <img class="blogPhoto" id="myImg1" src="/img/thumb1.jpg"  alt="Italy">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg2" src="/img/thumb2.jpg"  alt="Venice">
     </div>
     <div class="blogformat">
      <img class="blogPhoto" id="myImg3" src="/img/thumb3.jpg"  alt="Philippines">
     </div>

<!-- The Modal-->
     <div id="myModal" class="modal">
      <span class="close">&times;</span>
      <img class="modal-content" id="img01">
      <div id="caption"></div>
     </div>


     <script>
     // Get the modal
     var modal = document.getElementById("myModal");
                          
     // Get the image and insert it inside the modal - use its "alt" text as a caption
     var imgs = document.querySelectorAll("img");
     var modalImg = document.getElementById("img01");
     var captionText = document.getElementById("caption");
     
     // get all images and add an event listener, event target will be the clicked one
     imgs.forEach(img=>img.addEventListener("click",
       function (e) {
         modal.style.display = "block";
         modalImg.src = e.target.src;
         captionText.innerHTML = e.target.alt;
      }
     ))

     // Get the <span> element that closes the modal
     var span = document.getElementsByClassName("close")[0];
                          
     // When the user clicks on <span> (x), close the modal
     span.onclick = function() { 
             modal.style.display = "none";
             }
     </script>
                          
</div>