开发者

How do I insert attributes into an array?

Each element in $(some_selector) has attribute my_attr (which is a number).

I would like insert all these 开发者_运维问答attributes to array.

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


You can use .map() for this:

var arr = $("some_selector").map(function() {
            return $(this).attr("my_attr");
          }).get();

Or as a number, parse along the way:

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

Either of these return a JavaScript Array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜