在jQuery或Javascript中获取特定属性的最高和最低值 [英] Get the highest and lowest values of a certain attribute in jQuery or Javascript
本文介绍了在jQuery或Javascript中获取特定属性的最高和最低值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下div:
< div id =2class =maindiv> test< / DIV>
< div id =5class =maindiv> test< / div>
< div id =3class =maindiv> test< / div>
< div id =1class =maindiv> test< / div>
< div class =maindiv> test< / div>
< div id =4class =maindiv> test< / div>
如何在jQuery或Javascript中获得最高的id(5)和最低的id(1)?
解决方案
function minMaxId(selector){
var min = null,max = NULL; $(b)b $(selector).each(function(){
var id = parseInt(this.id,10);
if((min === null)||(id<如果((max === null)||(id> max)){max = id;}
});
return {min:min,max:max};
}
minMaxId('div'); // => {min:1,max:5}
I have the following divs:
<div id="2" class="maindiv">test</div>
<div id="5" class="maindiv">test</div>
<div id="3" class="maindiv">test</div>
<div id="1" class="maindiv">test</div>
<div class="maindiv">test</div>
<div id="4" class="maindiv">test</div>
How to get the highest id (5) and the lowest id (1) in jQuery or Javascript?
解决方案
function minMaxId(selector) {
var min=null, max=null;
$(selector).each(function() {
var id = parseInt(this.id, 10);
if ((min===null) || (id < min)) { min = id; }
if ((max===null) || (id > max)) { max = id; }
});
return {min:min, max:max};
}
minMaxId('div'); // => {min:1, max:5}
这篇关于在jQuery或Javascript中获取特定属性的最高和最低值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文