一个jQuery代码片段,用于获取页面上指定元素组的最高id 。 如果您要给容器元素提供ID,并且需要加载下一个高于当前任何元素ID的元素,则很有用。 例如,加载产品并将产品ID存储在容器div中,然后,当用户向下滚动页面时,它可以加载更多产品,但抓住最高ID传递给服务器端脚本以返回产品数据。
DOM结构示例
............
编码
//filtered by class, but you could loop all elements
var highest = 0, this_id;
$(".item").each( function(i,v)
{
this_id = parseInt($(this).attr('id'));
console.log(this_id+">"+highest+"="+(this_id > highest));
if (this_id > highest)
{
highest = this_id;
console.log('new highest = ' + highest )
}
});
console.log(highest);
From: https://www.sitepoint.com/jquery-highest-id-elements-page/