0

Basically, on clicking any image on a html page I want the id associated to be passed to a function. This is what I have tried. It seems I am making a minor mistake here as I am getting the first id passed no matter what image I click from the array. I tried $(this).attr("id") as well, but did not work.

            for(var i=0;i<jsonObj.length-1;i++){
                var rows = '';
                var bg_img = jsonObj[i].img;
                var bg_img = decodeURIComponent(bg_img);                    
                rows = "<img id='" + jsonObj[i].source_id + "' src='" + bg_img + "'/>";                 
                document.getElementsByClassName('subscription')[i].innerHTML = rows;
            }                           

            $("body").delegate(".subscription", "click", function() {
                //  var id = $(this).attr("id");
                    alert("Welcome Test " + $('img').attr("id"));
                return false;
            });
4

2 回答 2

1
$("img").click(function()
{
    var id = $(this).attr("id");
});
于 2013-07-28T20:37:21.730 回答
0

您的$('img')选择器不限于任何特定区域,因此它将为您提供整个页面上的第一个图像。

试试$('img',this)吧。

于 2013-07-28T20:37:27.553 回答