Get the highest and lowest values of a certain attribute in jQuery or Javascript

前端 未结 7 1083
鱼传尺愫
鱼传尺愫 2020-12-10 05:37

I have the following divs:

test
test
相关标签:
7条回答
  • 2020-12-10 06:19

    Create a global variable and compare it to each element using Math.max inside a loop.

    var maxval = Number.MIN_VALUE,
        minval = Number.MAX_VALUE;
    
    $('div').each(function () {
        var num = parseInt(this.id, 10) || 0; // always use a radix
        maxval = Math.max(num, maxval);
        minval = Math.min(num, minval);
    });
    
    console.log('max=' + maxval);
    console.log('min=' + minval);
    

    http://jsfiddle.net/mblase75/gCADe/

    0 讨论(0)
提交回复
热议问题