开发者

Cycle to get the source of a list of images

开发者 https://www.devze.com 2023-03-13 17:20 出处:网络
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.

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

精彩评论

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