简体   繁体   English

jQuery click事件导致循环仅执行一次

[英]Jquery click event causing loop to execute only one time

I want to show next five divs each time when i click show more button, 5 divs will be shown default when load, here is my code, 我想在每次单击“显示更多”按钮时每次显示下五个div,加载时默认显示5个div,这是我的代码,

var counterBox = 5;
$("#show_more_location").click(function() {
    $(".loading-container").show();
    for (var inc = 5; inc <= 5; inc++) {
        if(counterBox<50){
            counterBox = counterBox + 1;
            $(".location-box:nth-child("+counterBox+")").slideDown().addClass("show_box").delay(100);
        }  
    }
    $(".loading-container").delay(5000).hide();         
});

Problem is that when I click show more button, it loops only once, and stops. 问题是,当我单击“显示更多”按钮时,它仅循环一次,然后停止。 Only one div is showing ,I want to show 5 divs on show buttonclick. 仅显示一个div,我想在show buttonclick上显示5个div。

Can anyonehelp? 有人可以帮忙吗?

It seems you init your inc iterator with 5 , and get out of the loop when it's higher than 5 : 看来您使用5初始化了inc迭代器,并且当它高于5时退出了循环:

for (var inc = 5; inc <= 5; inc++) {
   ...
}

Try initializing it with 1 : 尝试使用1初始化它:

for (var inc = 1; inc <= 5; inc++) {
   ...
}

You're setting inc in the for loop to 5, then checking if it is lower than or is 5. So it runs once. 您将for循环中的inc设置为5,然后检查它是否小于或小于5。因此它运行一次。 Make inc 0 to run it 6 times. 使inc 0运行6次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM