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

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

I have the following divs:

test
test
7条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-10 06:18

    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}
    

    http://jsfiddle.net/qQvVQ/

提交回复
热议问题