[英]Modal images have troubles with classes
I got this modal script to display bigger images on my site. 我得到了这个模式脚本,以便在我的网站上显示更大的图像。 It worked well, but only with 1 image. 效果很好,但只有1张图片。 When i tried to add more images - script didn't worked, and nothing was happening. 当我尝试添加更多图像时-脚本不起作用,什么也没发生。 Can someone tell me what am I doing wrong? 有人可以告诉我我在做什么错吗?
Here's some code: 这是一些代码:
var modal = document.getElementById("myModal"); // Get the image and insert it inside the modal - use its "alt" text as a caption var img = document.getElementsByClassName("zdjecie"); 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"; }
<img class="zdjecie" style="max-height: 16rem;max-width: 16rem;" src="example.jpg">
When I was using ID's instead of classes it worked well - but i couldn't add more images - that's why i need to use classes. 当我使用ID而不是类时,它工作得很好-但我无法添加更多图像-这就是为什么我需要使用类。 Someone has any ideas? 有人有什么想法吗? I'd be greatful 我会很好
The document.getElementsByClassName
returns an array, which contains all the dom has the zdjecie
class. document.getElementsByClassName
返回一个数组,其中包含所有具有zdjecie
类的dom。
So you have to loop through this array to add the event, or put these images inside a container and use event-delegation
, which is a more efficient way. 因此,您必须遍历此数组以添加事件,或将这些图像放入容器中并使用event-delegation
,这是一种更有效的方法。 Because it just bind event once. 因为它只绑定一次事件。 More about event-delegation
: https://javascript.info/event-delegation ` 有关event-delegation
更多信息: https : // javascript.info/ event-delegation
Here is the code using event-delegation
. 这是使用event-delegation
的代码。
var handleImageClick = function(evt){ if(evt.target.className.indexOf('zdjecie') === -1) return; modal.style.display = "block"; modalImg.src = evt.target.src; captionText.innerHTML = evt.target.alt; } // Get the image and insert it inside the modal - use its "alt" text as a caption var modal = document.getElementById("myModal"); var imageContainer = document.getElementById("imageContainer"); var modalImg = document.getElementById("img01"); var captionText = document.getElementById("caption"); imageContainer.addEventListener('click', handleImageClick) // 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"; }
.zdjecie{ background-color: yellow; width: 100px; height: 100px; display: inline-block; }
<div id="imageContainer"> <img class="zdjecie" alt="1" style="max-height: 16rem;max-width: 16rem;" src="example1.jpg" /> <img class="zdjecie" alt="2" style="max-height: 16rem;max-width: 16rem;" src="example2.jpg" /> <img class="zdjecie" alt="3" style="max-height: 16rem;max-width: 16rem;" src="example3.jpg" /> <img class="zdjecie" alt="4" style="max-height: 16rem;max-width: 16rem;" src="example4.jpg" /> </div> <div id="myModal"> <span class="close">x</span> <img id="img01" style="max-height: 16rem;max-width: 16rem;" /> <p id="caption"></p> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.