Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用for/while循环Javascript/Jquery获得最终结果_Javascript_Jquery - Fatal编程技术网

无法使用for/while循环Javascript/Jquery获得最终结果

无法使用for/while循环Javascript/Jquery获得最终结果,javascript,jquery,Javascript,Jquery,我试图在和元素中添加一些html; 我想做的是计算div中的元素,然后如果该数字符合特定条件,则添加一些html //count how much divs with the class .col-lg are inside #looped var counting = $("#looped .col-lg").length; console.log(counting); //If statement that works; if (counting == 12 ) { insideLo

我试图在和元素中添加一些html; 我想做的是计算div中的元素,然后如果该数字符合特定条件,则添加一些html

//count how much divs with the class .col-lg are inside #looped
var counting = $("#looped  .col-lg").length;
console.log(counting);

//If statement that works;
if (counting == 12 ) {
  insideLoop.append('<div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div><div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div>');
}

//But i wanted to start from number 3 - 6 - 9 - 12 etc...
//计算类中有多少div。col lg在#循环中
var计数=$(“#looped.col lg”).长度;
控制台日志(计数);
//如果声明有效;
如果(计数==12){
insideLoop.append(“”);
}
//但我想从3-6-9-12号开始。。。

我想也许用for循环a就可以了,但我总是走到死胡同或是一个无休止的循环。

@Roysh的评论确实对我有用


我将(计数==12)替换为(计数%3==0)。

答案-您应该每三次迭代使用mod运算符

var counting = $("#looped  .col-lg").length;



if (counting % 3 === 0) {
  insideLoop.append('<div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div><div class="col-12 col-sm-6 col-md-4 col-lg servicios d-flex flex-column mt-sm-3 justify-content-center align-items-center"></div>');
}
var counting=$(“#looped.col lg”).length;
如果(计数%3==0){
insideLoop.append(“”);
}

您可能想看看这里:什么是
insideLoop
?您是否尝试每3次迭代渲染一次此div?这就是问题所在吗?@Mohammad insideLoop是一个我忘记添加的变量;var insideLoop=$(“#循环”)@罗伊斯:是的!!因此,首先它看起来是否有3个div l,如果是,则执行;否则找6个div,找9个div等等。。