jQuery - 从类的元素中获取属性的值列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2754021/
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 - get a list of values of an attribute from elements of a class
提问by Ankur
I have a class .object
which has an attribute called level
. I want to get a list of all the different values of level
on the page so I can select the highest one.
我有一个类.object
,它有一个名为level
. 我想获得level
页面上所有不同值的列表,以便我可以选择最高的值。
If I do something like:
如果我做这样的事情:
$(".object").attr("level")
... will that get me a list of values that are the values of the level attribute? I suspect not, but then how do you do something like that?
...这会给我一个值列表,这些值是 level 属性的值吗?我怀疑不是,但是你怎么做这样的事情?
Note:I don't want to select an HTML object for manipulation as is more common, rather I want to select values of the attribute.
注意:我不想像更常见的那样选择 HTML 对象进行操作,而是想选择属性的值。
EDIT: In order to get the highest "level" I have done this, but it doesn't seem to work. I will try the other suggested method now.
编辑:为了获得最高的“级别”,我已经这样做了,但它似乎不起作用。我现在将尝试其他建议的方法。
var highLevel=0;
$.each(".object[level]", function(i, value) {
if (value>highLevel) {
highLevel=value;
}
});
alert(highLevel);
回答by Kobi
$(".object").attr("level")
will just return the attribute of first the first .object
element.
$(".object").attr("level")
将只返回第一个.object
元素的属性。
This will get you an array of all level
s:
这将为您提供所有level
s的数组:
var list = $(".object").map(function(){return $(this).attr("level");}).get();
回答by harpo
First part of the question, getting the attribute values into an array. See this question
问题的第一部分,将属性值放入数组中。看到这个问题
jQuery get img source attributes from list and push into array
You would say
你会说
var levelArray = $('.object').map( function() {
return $(this).attr('level');
}).get();
Second part of the question , you can use this technique to get the highest value
问题的第二部分,您可以使用此技术获得最高值
var maxValue = Math.max.apply( Math, levelArray );
回答by artlung
<script type="text/javascript">
var max = 0;
jQuery(document).ready(function(){
jQuery('.object[level]').each(function(){
var num = parseInt($(this).attr('level'), 10);
if (num > max) { max = num; }
});
alert(max);
});
</script>
I'm assuming markup like this:
我假设这样的标记:
<div class="object" level="1">placeholder</div>
<div class="object" level="10">placeholder</div>
<div class="object" level="20">placeholder</div>
<div class="object" level="1000">placeholder</div>
<div class="object" level="40">placeholder</div>
<div class="object" level="3">placeholder</div>
<div class="object" level="5">placeholder</div>
For my code I get "1000" alerted to me.
对于我的代码,我收到了“1000”的提醒。
Here's another solution, combining several of the other replies from harpo, lomaxx, and Kobi:
这是另一个解决方案,结合了 harpo、lomaxx 和 Kobi 的其他几个回复:
jQuery(document).ready(function(){
var list = $(".object[level]").map(function(){
return parseInt($(this).attr("level"), 10);
}).get();
var max = Math.max.apply( Math, list );
alert(max);
});
回答by lomaxx
the selector
选择器
$(".object[level]")
will give you all the dom elements with class object
and an attribute level
.
将为您提供所有带有 classobject
和属性的 dom 元素level
。
Then you can just use the .each() method to iterate over the elements to get the highest value
然后你可以使用 .each() 方法迭代元素以获得最高值
回答by Albert Waninge
You can extend the functionality of Jqueryand add your own 'attrs' implementation.
您可以扩展 Jquery 的功能并添加您自己的“attrs”实现。
Add the following lines of code to your JavaScript file:
将以下代码行添加到您的 JavaScript 文件中:
jQuery.fn.extend({
attrs: function (attributeName) {
var results = [];
$.each(this, function (i, item) {
results.push(item.getAttribute(attributeName));
});
return results;
}
});
Now you can get the list of level values by calling:
现在您可以通过调用获取级别值列表:
$(".object").attrs("level")