jquery - 如何将自己的参数传递给 JQuery 分页插件中的回调函数?

标签 jquery pagination

根据JQuery pagination plugin's中callback的定义文件。看起来它只获取了自己的两个参数。那么,如何将额外的参数传递给回调函数。

callback

A callback function that is called when a user clicks on a pagination link. The function receives two parameters: the new page index and the pagination container (a DOM element). If the callback returns false, the event propagation is stopped. Default value: function(){return false;}. This callback function is essential for the functionality of the pagination! It should contain code that updates your content. For a fast user experience you should NOT load content via AJAX in this function. Instead, pre-load some content pages and switch between them with this function.

例如插件使用代码[来自其演示]:

附加分页插件:

$("#News-Pagination").pagination(122, {
    items_per_page:20,
    callback:pageselectCallback
});

回调函数:

    /**
     * Callback function that displays the content.
     *
     * Gets called every time the user clicks on a pagination link.
     *
     * @param {int}page_index New Page index
     * @param {jQuery} jq the container with the pagination links 
     *                 as a jQuery object
     */
     function pageselectCallback(page_index, jq){
       var new_content 
         = $('#hiddenresult div.result:eq(' + page_index + ')').clone();
       $('#Searchresult').empty().append(new_content);
       return false;
     }

非常感谢!!

最佳答案

使用自定义包装函数(这在 JS 中很常见)来实现这一点。

callback: function(jq1, jq2, ..., jqN) {
  return pageselectCallback(your1, your2, jq1, jq2, ..., jqN);
}

这里jq1jqN是jQuery分页插件给出的参数,而your1your2是参数您自己指定。

重要提示:由于省略号,上述内容不是有效的 JS 语法 ..., - 请根据您的情况进行更改

关于jquery - 如何将自己的参数传递给 JQuery 分页插件中的回调函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4894332/

相关文章:

JQuery 摘要页面问题

php - 对相关模型进行分页

javascript - 使用 Javascript 和 JQuery 计算网页元素的实际高度时出现问题?

jquery - 检查 div 是否可见

javascript - 带有下拉菜单的响应式导航栏

javascript - 如何检查 Jquery 是否被正确保存和嵌入?

php - 在 MySQL 分页结果中查找特定记录

php - 如何在 Codeigniter 中创建分页?

ruby - Jekyll - 当我的项目中有分页时出现错误

javascript - 我可以克隆、编辑和追加一个 div 吗?