将onclick事件添加到动态生成的元素jquery数组中

问题描述:

我在弹出式窗口的左侧有一个动态生成的缩略图,在单击该图像时,我希望该图像显示在右侧.

I have a dynamically generated thumbnail images on the left side of my popup and on click of the image i want that image to be displayed on the right hand side.

我正在使用以下代码:

    $.ajax({  
                    type: "POST",
                    url:  "/buildyourholiday/placeimages",
                    data: "srcid="+cityid,
                    success: function(data){
                        var objsrc = jQuery.parseJSON(data);
                        var src="";
                        $(".photoright").append().empty();
                        $.each(objsrc, function(index, obsrc){
                            src += '<ul>'+
                                '<li><a href="#"><img id="citythumb" onclick="sliderimage(this)" src="/images/city/thumbs/'+obsrc.img_filename+'" width="100" height="100"></a></li>'+
                                '</ul>';
                        });
                        $(".photoright").append(src);
                        function sliderimage(image)
{
    alert("sdf");
    alert(image);
}
                    }
                });

当我萤火虫时,我得到的SliderImage不是一个函数.当我在追加后使用onclick事件时,我只会得到第一张图像,但是有多张图像.我希望onclick对任何图像的点击起作用.怎么做?

When i firebug it i get sliderimage is not a function. When i use the onclick event after append i get only the first image, but there are multiple images. I want the onclick to work on the click of any image. How to go about it?

谢谢

id="citythumb"必须唯一,使用class,例如class="citythumb"

id="citythumb" must be unique, use class, like class="citythumb"

$(".photoright").on('click', 'img', function(e){
   e.preventDefault(); // for link
   alert(this);
});

更改id属性时,也可以将img替换为.citythumb

when you change id attribute, you could also replace img with .citythumb