jQuery - 从类的元素获取属性的值的列表 [英] jQuery - get a list of values of an attribute from elements of a class
问题描述
我有一个类 .object
,它有一个属性 level
。我想在页面上得到 level
的所有不同值的列表,以便我可以选择最高的一个。
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").attr("level")
...会得到一个值列表,这是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?
注意:我不想选择一个HTML对象更常见,而我想选择属性的值。
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.
编辑:为了获得最高的级别我做到了这一点, t似乎工作。现在我将尝试其他建议的方法。
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);
推荐答案
$ ).attr(level)
将只返回第一个 .object
元素的属性。
$(".object").attr("level")
will just return the attribute of first the first .object
element.
这将得到所有级别
的数组:
var list = $(".object").map(function(){return $(this).attr("level");}).get();
这篇关于jQuery - 从类的元素获取属性的值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!