开发者

Jquery to max number from a set of divs

Given the following HTML

<div class"myclass">10</div>
<div class"myclass">25</div>
<div class"myclass">50</div>
<div开发者_StackOverflow社区 class"myclass">20</div>

I want Jquery to return the maximum value found on divs with class:"myclass". (This is 50)

I thought of using .find.text() will be a good starting point but cant figure out exactly how,

Help is greatly appreciatted,

Thanks


My shot at it:

var max = 0;
$("div.myclass").each(function(){
    var value = parseInt($(this).text())
    if(value > max) max = value;
});
console.log(max);


var max = 0;
$('.myclass').each(function(){
    thisVal = parseInt($(this).text(), 10);
    if(thisVal > max) max = thisVal;
});
alert(max);


Well, something like

var $set = $('myclass'),
    max  = 0;
$.each($set, function(){
    if(parseInt(this.text()) > max)
       max = parseInt(this.text());
});

should do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜