javascript jQuery:如何计算所有匹配元素的最大属性值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7524583/
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
jQuery: How to calculate the maximal attribute value of all matched elements?
提问by Misha Moroshko
Consider the following HTML:
考虑以下 HTML:
<div class="a" x="6"></div>
<div class="a" x="9"></div>
<div class="a" x="2"></div>
...
<div class="a" x="8"></div>
How would you find the maximal x
value of all .a
elements ?
你将如何找到x
所有.a
元素的最大值?
Assume that all x
values are positive integers.
假设所有x
值都是正整数。
回答by Blender
Just loop over them:
只需循环它们:
var maximum = null;
$('.a').each(function() {
var value = parseFloat($(this).attr('x'));
maximum = (value > maximum) ? value : maximum;
});
回答by topek
I got another version:
我得到了另一个版本:
var numbers = $(".a").map(function(){
return parseFloat(this.getAttribute('x')) || -Infinity;
}).toArray();
$("#max").html(Math.max.apply(Math, numbers));
This uses the map function to extract the values of the x-Attributes, converts the object into an array and provides the array elements as function parameters to Math.max
这使用 map 函数来提取 x-Attributes 的值,将对象转换为数组并将数组元素作为函数参数提供给 Math.max
The Math.max trick was stolen from http://ejohn.org/blog/fast-javascript-maxmin/
Math.max 技巧是从http://ejohn.org/blog/fast-javascript-maxmin/窃取的
UPDATE
更新
add "|| -Infinity" to process the case correctly, when no attribute is present. See fiddle of @kubedan
添加“|| -Infinity”以在不存在属性时正确处理案例。见@kubedan的小提琴
回答by Ben Taliadoros
回答by Erick Petrucelli
I was doing some tests regarding this topic, and if performance matters, a old but gold simple for
would be better than jQuery.map()
, Math.apply()
and also Array.sort()
:
我正在做一些关于这个主题的测试,如果性能很重要,一个旧但金色的简单for
会比jQuery.map()
,Math.apply()
而且Array.sort()
:
var items = $(".a");
for (var i = 0; i < items.length; i++) {
var val = items.eq(i).prop('x');
if (val > max) max = val;
}
Here are the jsperftests: http://jsperf.com/max-value-by-data-attribute. Nothing really drastic, but interesting anyway.
以下是jsperf测试:http: //jsperf.com/max-value-by-data-attribute。没有什么特别激烈的,但无论如何都很有趣。
回答by nnnnnn
var max = null;
$('.a').each(function() {
var x = +($(this).attr('x'));
if (max === null || x > max)
max = x;
}
alert(max === null ? "No matching elements found" : "The maximum is " + max);
Note the unary +
operator to convert the attribute to a number. You may want to add some error checking to ensure it actually isa number - and that the attribute exists at all. You could change the selector to only select elements with the class and the attribute: $('.a[x]')
.
请注意+
将属性转换为数字的一元运算符。您可能想要添加一些错误检查以确保它实际上是一个数字 - 并且该属性根本存在。您可以将选择器更改为仅选择具有类和属性的元素:$('.a[x]')
。
回答by Kanishka Panamaldeniya
var max =0;
$('.a').each(function(){
if(parseFloat($(this).attr('x'))>max)
{
max = parseFloat($(this).attr('x')) ;
}
});
alert(max);