jQuery 或 Javascript 获取某个属性的最高和最低值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14775921/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 22:32:18  来源:igfitidea点击:

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

javascriptjqueryhtmlloops

提问by Michael Samuel

I have the following divs:

我有以下 div:

<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?

如何在 jQuery 或 Javascript 中获取最高 id (5) 和最低 id (1)?

采纳答案by maerics

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/

http://jsfiddle.net/qQvVQ/

回答by Rory McCrossan

First you need to create an array containing all the idvalues, then use Mathto get the highest/lowest:

首先,您需要创建一个包含所有id值的数组,然后使用Math获取最高/最低值:

var ids = $(".maindiv[id]").map(function() {
    return parseInt(this.id, 10);
}).get();

var highest = Math.max.apply(Math, ids);
var lowest = Math.min.apply(Math, ids);

Example fiddle

示例小提琴

Note the [id]attribute selector is required, otherwise 0is assumed for the missing value.

注意[id]属性选择器是必需的,否则0假设为缺失值。

回答by Explosion Pills

var min = Number.MAX_VALUE, max = Number.MIN_VALUE;
$(".maindiv").each(function () {
    var id = parseInt(this.id, 10);
    if (id > max) {
        max = id;
    }
    if (id < min) {
        min = id;
    }
});

回答by Wasula Kankanamge

var valArray = [];
$('.maindiv').each(function(){
    valArray.push(parseInt($(this).attr('id'), 10));
})
valArray.sort(function(a, b) { return a - b })

valArrayp[0] // lowest
valArrayp[valArrayp.length - 1] // highest`

Have not tested, should work though

没有测试过,不过应该可以用

回答by Licson

You can use the sort function to do it.

您可以使用排序功能来做到这一点。

function arrayify(obj){
    return [].slice.call(null,obj);
}
var all = arrayify(document.querySelectorAll('div[id]'));
var max = all.sort().pop();
var min = all.sort().reverse().pop();

This is way easier that using jQuery

这比使用 jQuery 容易得多

回答by Blazemonger

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

创建一个全局变量并将其与Math.max循环内部的每个元素进行比较。

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/

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

回答by James Donnelly

You should use data instead of id for this.

为此,您应该使用 data 而不是 id 。

<div data-value="2" class="maindiv">test</div>
<div data-value="5" class="maindiv">test</div>
<div data-value="3" class="maindiv">test</div>
etc.

Edit: I shortened my answer in favour of Rory McCrossan's accepted answer above.

编辑:我缩短了我的答案,以支持 Rory McCrossan 在上面接受的答案。