使用for循环创建分页jQuery

时间:2018-11-29 12:48:22

标签: jquery loops for-loop pagination

好,这是我的问题。我正在使用特定的加载函数,因此我需要为分页创建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个数字? 有人可以帮忙吗?

我的分页号

 <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>

1 个答案:

答案 0 :(得分:0)

嗯,这真的很容易,只需在纯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);
        });
});
}