我有来自 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/