提供ID's表示动力学元素

Give ID's to dynamical elements

本文关键字:表示 动力学 元素 ID 提供      更新时间:2023-09-26

因此:我通过JQuery将一些图像动态地放在一个列表中。我需要为每个图像分配不同的ID,因为我想在另一个分区中显示点击的图像。

这是我的JQ代码:

<script>
$.ajax({
    url: "Imagens/Trabalhos", //folder name
    success: function(data){
        var count = 0;
        $(data).find("a:contains(.jpg),a:contains(.png)").each(function(){
            // will loop through 
            var images = 'Imagens/Trabalhos/' +$(this).attr("href"); //get images names and set the path
            count++;
            var nome ='image'+count; //set the Suposed ID
            $('#ImageList').append('<li><a href="#" id="'+nome+'"><img src="' + images + '" width=90px height=120px></a></li>'); //Apply the images to the page, but the it dont recognise the id.
        });
    }
});
</script>

我知道如何在另一个div中显示图像,但我需要ID(对吗?(。我不是Jquery的专家,我想感谢所有的答案。

您不需要ID就可以在另一个div中显示图像。您只需要设置一个事件侦听器,如下所示:

$(function() {
    $('#ImageList').on('click', 'img', function() {
        $('#destination_div').html( $(this).clone() );
    });
});

工作JSFIDDLE演示