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

javascript - Using jQuery to select multiple values of attributes across a collection of objects - Stack Overflow

programmeradmin0浏览0评论

In this example I am selecting a bunch of 'A's within 'LI's (it doesn't really matter what I am selecting, just know I am returning a group of 'A' tags that all have the same "attribute structure").

I was wondering how I would go about returning a ma delimited list (or object/collection) of "attribute values". I was wondering if it can be done without a loop.

alert($(".bzsUserSelector-selected A"));
// this returns "[object]", which is expected

alert($(".bzsUserSelector-selected A").length);
// this returns "4", which is expected for my example

alert($(".bzsUserSelector-selected A").attr("myAttribute"))
// this returns "aaa", which is the value of the FIRST "myAttribute" only, I don't want that.
// I want something like this  "aaa, bbb, ccc, ddd"

I would like that to return an object of 4 items and just the 4 values of the "myAttribute" attribute.

I hope this is clear enough. Thanks in advance. - Mark

In this example I am selecting a bunch of 'A's within 'LI's (it doesn't really matter what I am selecting, just know I am returning a group of 'A' tags that all have the same "attribute structure").

I was wondering how I would go about returning a ma delimited list (or object/collection) of "attribute values". I was wondering if it can be done without a loop.

alert($(".bzsUserSelector-selected A"));
// this returns "[object]", which is expected

alert($(".bzsUserSelector-selected A").length);
// this returns "4", which is expected for my example

alert($(".bzsUserSelector-selected A").attr("myAttribute"))
// this returns "aaa", which is the value of the FIRST "myAttribute" only, I don't want that.
// I want something like this  "aaa, bbb, ccc, ddd"

I would like that to return an object of 4 items and just the 4 values of the "myAttribute" attribute.

I hope this is clear enough. Thanks in advance. - Mark

Share Improve this question edited Oct 6, 2009 at 10:48 Miguel Ping 18.3k23 gold badges91 silver badges137 bronze badges asked Jun 15, 2009 at 17:51 mschmidt42mschmidt42 1,0951 gold badge9 silver badges12 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Well, there are a lot of ways to do this, butthis particular way is relatively concise and makes use of the makeArray and map functionality in jQuery.

$('li').map(function() {
  return $(this).attr("myAttribute")
}).get().join(',')
发布评论

评论列表(0)

  1. 暂无评论