Get the attribute value of each element from a jQuery set, into an array

前端 未结 4 1224
Happy的楠姐
Happy的楠姐 2021-02-08 02:25

How can I get all attributes (e.g. href) of all elements matching a jQuery selector?

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 03:09

    One-liner with ES6 arrow functions, using jQuery .map():

    const ids = $('a.someClass').map((i, el) => el.getAttribute('href')).get();
    
    console.log(ids);
    
    t1
    t2
    t3

提交回复
热议问题