jquery pagination through multiple ul lists

痞子三分冷 提交于 2019-12-23 04:54:10

问题


I can't get this script to work well. Can someone help me? First, I would like to make it that I can use it for multiple <div> and <ul> elements. One other thing, I would like to make it so that if I have more than 5 <li> elements it appends a "next" button and then on page 2, it appends "prev" and "next" buttons. Also, if on the last page "next" button shouldn't be seen. Here is my current code:

 $('div').each(function(){
    $(this).append('<a class="prev">prev</a> | <a class="next">next</a>');
    $(this).find('ul li:gt(4)').hide();

    $(this).find('.next').click(function() {
        var last = $('ul').children('li:visible:last');
        last.nextAll(':lt(5)').show();
        last.next().prevAll().hide();
    });

     $(this).find('.prev').click(function() {
        var first = $('ul').children('li:visible:first');
        first.prevAll(':lt(5)').show();
        first.prev().nextAll().hide()
    });

});

Working example http://jsfiddle.net/e6sP7/3/


回答1:


You can do something like this (Fiddle):

function check_navigation_display(el) {
    //accepts a jQuery object of the containing div as a parameter
    if ($(el).find('ul').children('li').first().is(':visible')) {
        $(el).children('.prev').hide();
    } else {
        $(el).children('.prev').show();
    }

    if ($(el).find('ul').children('li').last().is(':visible')) {
        $(el).children('.next').hide();
    } else {
        $(el).children('.next').show();
    }    
}

$('div').each(function () {
    $(this).append('<a class="prev">prev</a> | <a class="next">next</a>');
    $(this).find('ul li:gt(4)').hide();

    check_navigation_display($(this));

    $(this).find('.next').click(function () {
        var last = $(this).siblings('ul').children('li:visible:last');
        last.nextAll(':lt(5)').show();
        last.next().prevAll().hide();
        check_navigation_display($(this).closest('div'));
    });

    $(this).find('.prev').click(function () {
        var first = $(this).siblings('ul').children('li:visible:first');
        first.prevAll(':lt(5)').show();
        first.prev().nextAll().hide()
        check_navigation_display($(this).closest('div'));
    });
});



回答2:


Just cache your reference to e.g. var foo = $(this); so it points to the proper group and you're fine. The update your first and last reference to use foo with var last = $('ul',foo).children('li:visible:last');:

jsFiddle example

$('div').each(function () {
    var foo = $(this);
    $(this).append('<a class="prev">prev</a> | <a class="next">next</a>');
    $(this).find('ul li:gt(4)').hide();
    $(this).find('.next').click(function () {
        var last = $('ul',foo).children('li:visible:last');
        last.nextAll(':lt(5)').show();
        last.next().prevAll().hide();
    });
    $(this).find('.prev').click(function () {
        var first = $('ul',foo).children('li:visible:first');
        first.prevAll(':lt(5)').show();
        first.prev().nextAll().hide()
    });
});


来源:https://stackoverflow.com/questions/23636161/jquery-pagination-through-multiple-ul-lists

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!