从jQuery集中获取每个元素的属性值到数组中

zii*_*web 9 jquery attributes jquery-selectors

如何获得href与jQuery选择器匹配的所有元素的所有属性(例如)?

rah*_*hul 28

就像是

var idArray = $(".someClass").map(function(){
    return this.id
}).get().join(',');
Run Code Online (Sandbox Code Playgroud)

工作演示


g.d*_*d.c 20

也许这样的事情?

var ids = [];

$('.myClass').each(function () {
  ids.push($(this).attr('id')); // ids.push(this.id) would work as well.
});
Run Code Online (Sandbox Code Playgroud)

  • @DavidThomas:`this.id` 仅适用于 `id` 属性,因为 `this` 是一个 DOM 注释。对于一般情况,例如`href`,您需要`this.getAttribute('href')` 或`$(this).attr('href')`。 (2认同)