开发者

Cycle to get the source of a list of images

I have a list with 8 thumbnails images. When I click in one of them the bigger image gets the source of the that thumbnail. I can do this one by one.

$('#img_1').click(function(){
    var temp = $('#img_1开发者_运维百科').attr('src');
    $('#bigger_image').attr('src', temp);
});

I tried to use a for loop but I always get the last thumbnail source

for(var i=0; i<$('#thumbsContainer').children().length;i++){
    $('#img_'+i).click(function(){
        var temp = $('#img_'+i).attr('src');
        $('#bigger_image').attr('src', temp);
    });
}

How should I do it in a once?


You can also loop over the images using the each function.

$('#thumbsContainer [id^=img_]').each(function(){
    $(this).click(function(){
        $('bigger_image').attr('src', $(this).attr('src'));
    });
}


Try this

for(var i=0; i<$('#thumbsContainer').children().length;i++){
    $('#img_'+i).click(function(){
        $('#bigger_image').attr('src', $(this).attr('src'));
    });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜