简体   繁体   English

在div之类的图片库中显示图片

[英]Display image in a div like image gallery

I want to display a selected image from table rows inside a div. 我想显示div中表行中的选定图像。 i am using a for loop to populate the table but now i find it hard to display the selected image inside the div as the id's are dynamic. 我正在使用for循环填充表格,但是现在我发现很难在div中显示所选图像,因为id是动态的。 Here is my for loop 这是我的循环

for (i=1; i < my_image.length-1; i++) {

            if (i%3==0) {

                str += "<td><a 

href='"+my_image[i]+"'onclick='displayimg()'id=/image"+my_image[i] +"/"+" class='popup-

open'><img src='"+my_image[i]+"'"+

                "width='80' height='65'></a></td></tr><tr>";

            }

            else {

                str+= "<td><a href='"+my_image[i]+"'class='popup-open'><img src=' 

"+my_image[i]+

"' width='80' height='65'></a></td>";

            }
        }

how can i display these image in a div content on user click. 当用户单击时,如何在div内容中显示这些图像。

If i understand you correctly, the table will have unique Ids that are dynamic, but the div will be static and will hold whichever image is selected, much like a larger view of the image that is clicked? 如果我正确理解您的意见,该表格将具有动态的唯一ID,但是div将是静态的,并且将容纳所选的任何图像,就像单击该图像的较大视图一样?

If so, try this... 如果是这样,请尝试...

For the javascript try this code: 对于javascript,请尝试以下代码:

function showImage(container, image)
{
    var imageTag = document.getElementById(container);
    imageTag.src = image;
}

In the table you're making, for the onclick method in the tag 在您制作的表格中,针对标记中的onclick方法

<img onclick="showImage('IDofTheImageTagToRenderImage', this.getAttribute('src')" />

with that you don't need to know which Id holds the image, you only need to have the src information and can pass that to a javascript method to populate the div's image tag that you want to show the selected image. 这样,您就不需要知道哪个ID可以保存图片,您只需拥有src信息,就可以将其传递给javascript方法以填充要显示所选图片的div的图片标签。 Since (to my understanding) that would be a static Id value for that div, and not a dynamically generated one. 因为(据我所知)那将是该div的静态ID值,而不是动态生成的值。

If you are placing this in a popup div, i have something for that also and can modify the answer to fit. 如果您将其放置在弹出式div中,我也有相应的选择,可以修改答案以适合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM