javascript - 如何隐藏后面的页面并在 jquery ajax 响应中显示分页?

标签 javascript jquery ajax

我有来自 ajax 响应的分页列表。如何根据限制器隐藏后面的页面并显示分页。

如果限制器是 5,则必须只显示 10 个页面链接并隐藏后面的页面。

1 2 3 4 5 6 7 8 9 10 .. 下一个

11 12 13 14 15..下一个

我试图从 ajax 响应中计算 li 的长度,但它没有显示计数。如何获取计数以及隐藏和显示后面的页面。

输出:

next_page_num: <li>1</li><li>2</li><li>3</li><li>5</li><li>6</li><li>7</li>..



  success: function(response){  
                $("#listing_products").html("");
                $("#listing_products").html(response.html_content);
                $("ul.pagination").html("");
                $("ul.pagination").html(response.next_page_num);

                if(page_limit==5){

                    var product_listing_count = response.next_page_num;



                    if($(product_listing_count).length>8){

//How to hide after 8th pages and hide later pages add next to 8th page

 ));

                    }               

                    $("ul.pagination").html("");
                    $("ul.pagination").html(response.next_page_num);

                }

最佳答案

这是因为 $.fn.find函数遍历元素的子节点,所以如果没有像 <ul> 这样的父节点没有 child ...

然后:

$('<ul><li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li></ul>')
    .find('li').length;

returns 6

和:

$('<li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li>')
    .find('li').length; 

returns 0

获取长度的一个简单方法是 split/length :

'<li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li>'
    .split('<li>').length - 1; 
 //-1 because there will be always a empty entry in the array

returns 6

或者像 Adelin 所建议的那样,使用 RegExp:

 '<li>1</li><li>2</li><li>3</li><li>4</li><li>5</li><li>6</li>'
      .match(/(<li>)/g).length

returns 6

关于javascript - 如何隐藏后面的页面并在 jquery ajax 响应中显示分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51284124/

相关文章:

javascript - 按日期范围过滤数据Javascript-jquery

javascript - 如何获取活跃等级值?

php - AJAX/jQueryalert() 有效,.html() 或 .text() 无效

javascript - 提交后清除我的表单输入

javascript - 使用滚轮放大/缩小

javascript - 从 <li> 中删除项目

jquery - Rails 4 ajax 充当可投票的涡轮链接投票计数不更新

javascript - 在 jquery 中通过 $.ajax 提交表单后获取成功信息

javascript - 如果用户未使用 Nodejs 登录,如何添加注销按钮

javascript - 用于测试日期格式的正则表达式