如何将属性插入数组?

Mis*_*hko 2 javascript jquery attributes

每个元素$(some_selector)都有属性my_attr(这是一个数字).

I would like insert all these attributes to array.

What would be the easiest way to do this using jQuery ?

Nic*_*ver 5

你可以使用.map()这个:

var arr = $("some_selector").map(function() {
            return $(this).attr("my_attr");
          }).get();
Run Code Online (Sandbox Code Playgroud)

或者作为一个数字,一路上解析:

var arr = $("some_selector").map(function() {
            return parseInt($(this).attr("my_attr"), 10);
          }).get();
Run Code Online (Sandbox Code Playgroud)

其中任何一个都返回一个JavaScript数组.