最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Return matched elements' attribute ar array using jQuery - Stack Overflow

programmeradmin2浏览0评论

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();
发布评论

评论列表(0)

  1. 暂无评论