【问题标题】:Create Pagination jquery with for loop使用 for 循环创建分页 jquery
【发布时间】:2018-11-29 12:48:21
【问题描述】:

好的,这是我的问题。我正在使用特定的加载函数,我需要为分页创建 for 循环,以便动态加载内容而不是重复代码块。

 $('.teoBtn_1').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t1" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

    $('.teoBtn_2').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t2" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

    $('.teoBtn_3').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t3" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

    $('.teoBtn_4').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t4" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

    $('.praBtn_1').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t5" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

        $('.praBtn_2').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t6" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});


        $('.praBtn_3').click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t7" + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});

我重复点击 teoButton_1 2 和 3 等的所有动作...以加载 _t1、_t2 和 _t3,我如何将其转换为 for 循环而无需每次指定 1 2 或 3 个数字? 有人可以帮忙吗?

我的分页HTML:

 <div class="pagination">
     <a class="at teoBtn_1" href="#">1</a>
     <a class="at teoBtn_2" href="#">2</a>
     <a class="at teoBtn_3" href="#">3</a>
     <a class="at teoBtn_4" href="#">4</a>
     <a class="ap teoBtn_5" href="#">5</a>
     <a class="ap teoBtn_6" href="#">6</a>
     <a class="ap teoBtn_7" href="#">7</a>
  </div>

【问题讨论】:

  • 你能提供那个html吗?
  • 我可以提供分页html,但不能提供所有加载的.html文档

标签: jquery loops for-loop pagination


【解决方案1】:

嗯,真的很简单,只需要在纯javascript中使用:

for (i = 0; i < 7; i++) { // will loop 7 times
$('.teoBtn_' + i).click(function() {
    hideAll();
    $('#contentorTopico').load(unidadeSessao + "_t" + i + '.html', function(){
        $('#contentorTopico').show();
        $('.nome_topico').text(topicoName);
        });
});
}

【讨论】:

  • 太完美了,谢谢 =)
  • 您可以改进它,将for (i = 0; i &lt; 7; i++) 更改为(i = 0; i &lt; $('.pagination a').length; i++) 以检测分页内的链接数量。
猜你喜欢
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 1970-01-01
  • 2023-03-11
  • 1970-01-01
  • 2021-04-15
相关资源
最近更新 更多