问题描述
我有一个类 .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")
...这会给我一个作为级别属性值的值列表吗?我怀疑不是,但是你怎么做这样的事情?
... 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.
为了获得最高的级别",我这样做了,但它似乎不起作用.我现在将尝试其他建议的方法.
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);
推荐答案
$(".object").attr("level")
只会返回第一个的属性.object
元素.
$(".object").attr("level")
will just return the attribute of first the first .object
element.
这将为您提供所有 level
的数组:
This will get you an array of all level
s:
var list = $(".object").map(function(){return $(this).attr("level");}).get();
这篇关于jQuery - 从类的元素中获取属性值的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!