遍历类为"blah"的所有元素,并找到最高的id值 [英] Loop through all elements with class 'blah' and find the highest id value
本文介绍了遍历类为"blah"的所有元素,并找到最高的id值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆元素,例如:
<div id="car-123" class="blah">..</div>
我想遍历所有这些对象并获得最高ID,即123
I want to loop through all of them and get the highest ID i.e. 123
该怎么做?
下面是正确的最佳方法吗?
Is below correct and the best way?
$(".blah").each(function() {
var id = $(this).attr('id').split('-')[0];
if( id > newid)
newid = id;
});
推荐答案
我会这样做:
var max = 0;
$(".blah").each(function(){
num = parseInt(this.id.split("-")[1],10);
if(num > max)
{
max = num;
}
});
大多数人会这样做.
这篇关于遍历类为"blah"的所有元素,并找到最高的id值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文