Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.2k views
in Technique[技术] by (71.8m points)

javascript - dynamically displaying images for Lightbox image ejs

I'm trying to dynamically load images into my table, which I got to work, but I am trying to get it to work where if I click the image, a Lightbox pops up that makes it show the image big. I cannot get this to work.

ejs:

<td>
<input id="<%= data[i].IMAGE %>" type="button" src="<%= data[i].IMAGE %>" alt="ORIGINAL PRODUCT IMAGE" value="See Current Image" width="300" height="200">
</td>

js:

// 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;
                    modalImg.alt = this.alt;
                    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";
                }

it only works for the very first image in my table, not sure how to fix. thanks


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

you are getting img by id, you should loop through all the imgs you have in the DOM and add event to each one of them.

can be dont that way: add class to the element with id = myImg, for this example it will be class="myImgLightBox"

// 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.getElementsByClassName('myImgLightBox');
                var modalImg = document.getElementById("img01");
                var captionText = document.getElementById("caption");
                for(let img of imgs){
                 img.onclick = function(){
                     modal.style.display = "block";
                     modalImg.src = this.src;
                     modalImg.alt = this.alt;
                     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";
                }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...