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'));
});
}
精彩评论