Javascript 为“图像模式”选择多个元素

Javascript 为“图像模式”选择多个元素,javascript,modal-dialog,selector,getelementbyid,Javascript,Modal Dialog,Selector,Getelementbyid,我想使用一个选择器来选择页面上的每个图像,以便将其放入模式,而无需为每个图像重写整个脚本。我觉得这是一个愚蠢的问题,但我一直在尝试不同的事情。现在我使用var img=document.getElementById(“article01”);选择一个图像,因为这是使用getElementById所能做的全部。因此,我想选择列出的两个图像,这样我就不会为每个图像重写整个脚本,因为页面上将有更多的图像。我尝试使用getelementbyclass和tagname,但我想我被卡住了 HTML: <

我想使用一个选择器来选择页面上的每个图像,以便将其放入模式,而无需为每个图像重写整个脚本。我觉得这是一个愚蠢的问题,但我一直在尝试不同的事情。现在我使用var img=document.getElementById(“article01”);选择一个图像,因为这是使用getElementById所能做的全部。因此,我想选择列出的两个图像,这样我就不会为每个图像重写整个脚本,因为页面上将有更多的图像。我尝试使用getelementbyclass和tagname,但我想我被卡住了

HTML:

<!-- Trigger the Modal -->
    <img id="article01" src="/images/article1.PNG" alt="" style="width:100%;max-width:300px">
    <img id="article01-2" src="/images/article1-2.PNG" alt="" style="width:100%;max-width:300px">
<!-- The Modal -->
    <div id="modal1" 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>

&时代;
关闭模态的元素
var span=document.getElementsByClassName(“关闭”)[0];
//当用户单击(x)时,关闭模式对话框
span.onclick=函数(){
modal.style.display=“无”;
}

您可以使用
document.querySelectorAll()
获取所有图像的。例如,如果您创建了这样的图像列表:

    <script>
    // Get the modal
    var modal = document.getElementById("modal1");

    *var img = document.getElementById("article01");*
    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";
    }

</script>

这里是一个链接,显示了如何执行此操作的演示…

您可以使用
document.queryselectoral()
获取所有图像的。例如,如果您创建了这样的图像列表:

    <script>
    // Get the modal
    var modal = document.getElementById("modal1");

    *var img = document.getElementById("article01");*
    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";
    }

</script>

这里有一个链接,展示了如何做这件事的演示…

这是我从你的帖子中得到的。我想你要找的是document.querySelector()


这是我从你的帖子里得到的。我想你要找的是document.querySelector()


我现在要实施这个。非常感谢。我现在要实施这个。非常感谢。
document.querySelectorAll('img.selectable')
  .forEach((img) => {
    img.addEventListener('click', (e) => showModal(e.target)); //or use img.onclick = ...
  });
<!-- Trigger the Modal -->
<img alt="this is a caption" src="https://livebrooks.com/wp-content/uploads/2017/07/fpo.gif" style="width:100%;max-width:300px">
<img alt="this is the second caption" src="https://livebrooks.com/wp-content/uploads/2017/07/fpo.gif" style="width:100%;max-width:300px">
<!-- The Modal -->
<div id="modalWrap" class="modal">
  <!-- The Close Button -->
  <span class="close" onclick="document.querySelector('#modalWrap').remove()">&times;</span>

</div>