Jquery:对超过 1 个 div 中的每组元素进行递增

发布于 2024-08-25 02:39:56 字数 1242 浏览 7 评论 0原文

我正在制作 Jquery 幻灯片。它列出了缩略图,单击缩略图时,会显示大图像作为叠加层。为了将拇指与大图像匹配,我向每个缩略图和大图像添加属性。这些属性包含一个数字,该数字将每个拇指与其相应的大图像相匹配。当页面上存在一张幻灯片时它会起作用。但如果存在多个幻灯片,我希望它能够工作。

这是向拇指和大图像添加属性的代码:

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);
});

这有效。为了在页面上有两个幻灯片时使增量工作,我想我可以将此代码粘贴到每个函数中...

$('.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);
    });
});

问题是增量运算符不会为第二个幻灯片 div (.slideshow) 重置,所以我最终在第一个 .slideshow div 中的拇指编号为 1,2,3 等。第二个 .slideshow div 中的拇指编号为 4,5,6 等。我如何在第二个 .slideshow 中制作数字div重置并重新从1开始?

这实在是很难简明扼要地解释清楚。我希望有人能明白要点。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难以启齿的温柔 2024-09-01 02:39:56

在每个中,下降一个级别,确保它的作用域为每个幻灯片,如下所示:

$('.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++);
    });
});

这将每个 .slideshow_content 块内的它们设置为 0 并在其中循环,而不是整体设置然后循环遍历所有块。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文