如何从一个jQuery对象元素获得属性值的数组 [英] How to get an array of attribute value from elements in a jQuery object
问题描述
我使用的元素自定义属性与我自己的类。我试图返回自定义属性的值类的所有元素。
我用jQuery来找到类的元素,和jQuery会在对象在数组中。
VAR选项卡= $('li.tab_item');
现在,我不得不在数组中的对象,我想自定义属性返回值阵列的所有成员。
如何才能做到这一点?
VAR tab_attribs = $('li.tab_item')。图(函数(){
返回$(本).attr(custom_attribute);
});
这会给你的自定义属性值的数组。当然,你可以做到这一点更为传统的:
VAR tab_attribs = [];
$('li.tab_item')。每个(函数(){
tab_attribs.push($(本).attr(custom_attribute));
});
无论如何,你应该利用数据 - *
属性是HTML5提供:
<李班=tab_item数据富=一些自定义的数据>
和(见 jQuery的数据()
)
$('li.tab_item')的数据(富)。 // - > 一些自定义的数据
I use a custom attribute in elements with my own class. I'm trying to return the value of custom attribute for all elements of the class.
I used jQuery to find the elements by class, and jQuery places the object in an array.
var tabs = $('li.tab_item');
Now that I'd have the objects in an array, I would like to return the value for the custom attribute for all the array's members.
How can this be done?
var tab_attribs = $('li.tab_item').map(function () {
return $(this).attr("custom_attribute");
});
This will give you an array of the custom attribute values. Of course, you can do this more traditionally:
var tab_attribs = [];
$('li.tab_item').each(function () {
tab_attribs.push( $(this).attr("custom_attribute") );
});
Anyway, you should probably make use of the data-*
attributes that HTML5 provides:
<li class="tab_item" data-foo="some custom data">
and (see jQuery data()
):
$('li.tab_item').data("foo"); // -> "some custom data"
这篇关于如何从一个jQuery对象元素获得属性值的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!