jQuery分页插件事件目标

时间:2015-08-20 14:44:04

标签: javascript jquery pagination

我正在使用这里看到的jQuery分页插件:

http://esimakin.github.io/twbs-pagination/

正如您在文档中看到的那样,有一个部分用于"同步分页元素"。我正在使用此功能在我的内容的底部和顶部放置分页控件。

但是,当单击底部分页元素时,我想实现一些特殊的滚动行为,不应该单击顶部分页元素。

我认为我可以在event回调中使用onPageClick参数,但是,似乎无论我点击顶部还是底部分页,事件总是将顶部分页列为其顶部分页currentTarget。为什么即使我点击底部分页控件也会发生这种情况?这是一个小提琴演示:

http://jsfiddle.net/flyingL123/qerexw0L/1/

HTML

<div class="text-center">
    <ul class="sync-pagination pagination-sm pagination" id="top"></ul>
    <div id="sync-example-page-content" class="well"></div>
    <ul class="sync-pagination pagination-sm pagination" id="bottom"></ul>
</div>

JS

$('.sync-pagination').twbsPagination({
    totalPages: 20,
    onPageClick: function (evt, page) {
        $('#sync-example-page-content').text('Page ' + page);
        console.log(evt);
    }
});

1 个答案:

答案 0 :(得分:0)

请在这里找到一个工作小提琴:http://jsfiddle.net/2vL4addq/

您需要解决此问题,因为类名称会为您提供第一个条目(在您的情况下,您的ID =顶部,而不是底部)。要定位底部,您需要执行一些自己的绑定。

我创建了一个函数setupPaginators(),它在doc ready期间使用,当页面按钮单击更改时保持顶部和底部同步(不使用插件默认行为)。

setupPaginators(1);

function setupPaginators(pageNumber){ 
    // Default options for page.   
    var opts = {
        totalPages: 20,
        onPageClick: function (evt, page) {
            $('#sync-example-page-content').text('Page ' + page);
            console.log(evt);
            if (($(this).attr("id") == "bottom")){
                // Put your special bottom code here.
                alert("bottom was clicked");   
            }
            setupPaginators(page);
        }
    };

    // Remove existing top.
    $('#top').empty();
    $('#top').removeData("twbs-pagination");
    $('#top').unbind("page");
    // Bind new top and override the startPage.
    $('#top').twbsPagination($.extend(opts, {
        startPage: pageNumber
    }));

    // Remove existing bottom.
    $('#bottom').empty();
    $('#bottom').removeData("twbs-pagination");
    $('#bottom').unbind("page");
    // Bind new bottom and override the startPage.
    $('#bottom').twbsPagination($.extend(opts, {
        startPage: pageNumber
    }));
}