模态图像-使用目录图像-W3 Schools Modal Image mod

时间:2018-10-05 03:42:33

标签: javascript html5 modal-dialog

我正在使用W3 Schools的Modal Image教程。在本教程中,它使用单击的图像作为显示的图像。我希望模式显示我目录中的文件,而不是单击图像。我计划在同一页面上具有该模式的多个实例,这些实例链接到目录中的单独文件。

如何从目录中获取元素,而不是var“ img01” /“ myImg”

HTML          

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

  <!-- The Close Button -->
  <span class="close">&times;</span>

  <!-- Modal Content (The Image) -->
  <img class="modal-content" id="img01">

  <!-- Modal Caption (Image Text) -->
  <div id="caption"></div>

 </div> 

JS

<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('myImg');
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;
}

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

0 个答案:

没有答案