微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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>这样的父节点没有孩子……

然后:

$('<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

获得长度的简单方法是分割/长度:

'<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 sugested一样,使用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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐