How to get an array of attribute value from elements in a jQuery object

前端 未结 3 906
温柔的废话
温柔的废话 2021-01-12 02:04

I use a custom attribute in elements with my own class. I\'m trying to return the value of custom attribute for all elements of the class.

I used jQuery to find th

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-12 02:37

    Use .map():

     $("li.tab_item").map(function (){
        return this.getAttribute("myAttribute");
     });
    

    That gives you an Array of values wrapped in a jQuery object. If you want to get the Array, call .get(), ie .map(...).get().

    By the way, you can also select the elements by attribute instead of class:

    $("[myAttribute]")
    

    This will return all elements on the page that have a myAttribute attribute.

提交回复
热议问题