开发者

Jquery: Incrimentation for each set of elements in more than 1 div

I'm making a Jquery slideshow. It lists thumbnails, that when clicked on, reveal the large image as an overlay. To match up the thumbs with the large images I'm adding attributes to each thumbnail and large image. The attributes contain a number which matches each thumb to its corresponding large image. It works when one slideshow is present on a page. But I want it to work if more than one slideshow is present.

Here's the code for adding attributes to thumbs and large images:

thumbNo = 0;
largeNo = 0;
$('.slideshow_content .thumbs img').each(function() {            
   thumbNo++;
   $(this).attr('thumbimage', thumbNo);
   $(this).attr("title", "Enter image gallery");
});
$('.slideshow_content .image_container .holder img').each(function() {   
   largeNo++;
   $(this).addClass('largeImage' + largeNo);
});

This works. To make the incrementation work when there are two slideshows on a page, I thought I could stick this code in an each function...

$('.slideshow').each(function() {
    thumbNo = 0;
    largeNo = 0;
    $(this + '.slideshow_content .thumbs img').each(function() {            
       thumbNo++;
       $(this).attr('thumbimage', thumbNo);
       $(this).attr("title", "Enter image gallery");
    });
    $(this + '.slideshow_content .image_container .holder img').each(function() {   
       largeNo++;
       $(this).addClass('largeImage' + largeNo);
    });
});

The problem with this is that the incrimenting operator does not reset for the second slideshow div (.slideshow), so I end up with thumbs in the first .slideshow div being numbered 1,2,3 etc.. and thumbs in the second .slideshow div being numbered 4,5,6 开发者_运维技巧etc. How do I make the numbers in the second .slideshow div reset and start from 1 again?

This is really hard to explain concisely. I hope someone gets the gist.


In your each, go down a level, make sure it's scoped to each slideshow like this:

$('.slideshow_content').each(function() {
    thumbNo = 0;
    largeNo = 0;
    $(this).find('.thumbs img').each(function() {
       $(this).attr('thumbimage', thumbNo++);
       $(this).attr("title", "Enter image gallery");
    });
    $(this).find('.image_container .holder img').each(function() {   
       $(this).addClass('largeImage' + largeNo++);
    });
});

This sets them to 0 inside each .slideshow_content block and looping inside it instead of setting it overall and then looping through all blocks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜