admin管理员组

文章数量:1208155

In jQuery, it is easy to select elements as array.

$("a"); // return as elements array of anchors

But is it possible to select matched elements' attributes as array?

Currently I need to do something like...

links = [ ];

$("a").each(function() {

href = $(this).attr("href");
links.push(href); 

});

Are there any better method to fill the variable links with href of the all matched anchors?

In jQuery, it is easy to select elements as array.

$("a"); // return as elements array of anchors

But is it possible to select matched elements' attributes as array?

Currently I need to do something like...

links = [ ];

$("a").each(function() {

href = $(this).attr("href");
links.push(href); 

});

Are there any better method to fill the variable links with href of the all matched anchors?

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jul 28, 2010 at 16:34 HowardHoward 19.8k36 gold badges115 silver badges187 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 19

Use $.map like so:

var links = $('a').map(function() { return this.href }).get()
var links = $("a").map(function(){return $(this).attr("href")}).get();

本文标签: javascriptReturn matched elements39 attribute ar array using jQueryStack Overflow