javascript - 循环遍历每个容器中的所有元素

标签 javascript jquery loops

我需要循环遍历每个容器中的所有项目,每次循环到达新容器时都从 1 开始。

编辑:添加了 JSFiddle http://jsfiddle.net/ntv4k4LL/

例如

Container

 - 1 
 - 2
 - 3

Container

 - 1 
 - 2
 - 3

但目前正在发生

Container

 - 1 
 - 2
 - 3

Container

 - 4 
 - 5
 - 6

这是我的代码:

$('.index-slide-rel').each(function(){

     $(this).children('article').each(function(i,e){

         col++;

         if(i > 0 && i%cols === cols-1) {
             sx= 0;
             row++;
             col=1;
             sy += previewElementHeight;
         }                  
     })                     
});

最佳答案

$('.index-slide-rel').each(function(i, container) {
    // here, i should take the value 0 to 1, considering you have 2 containers.
    $(container).children('article').each(function(j, element) {
        // here, j should take the value 0 to 2, considering you have 3 elements.
    });                 
});

关于javascript - 循环遍历每个容器中的所有元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476997/

相关文章:

javascript - 为什么我的条件没有做任何事情?

jquery - 当元素完全在 View 中时,使用 .each() 运行单独的函数

javascript - 导出数据表时排除/隐藏某些列

python - 最小化循环迭代的多变量函数

python - 循环 python openpyxl。如何在单元格中添加循环

javascript - 在原始文本字符串中的字符周围插入范围和类

getElementById 无法使用 JavaScript 对象引用

javascript - 改变对插入值(value)的关注

javascript - 数组值检查 JavaScript 中是否出现(使用箭头函数?)

loops - MailJet 嵌套循环