jQuery - get a list of values of an attribute from elements of a class

后端 未结 5 2058
北荒
北荒 2020-12-04 07:34

I have a class .object which has an attribute called level. I want to get a list of all the different values of level on the page so I

5条回答
  •  执念已碎
    2020-12-04 08:35

    You can extend the functionality of Jquery and add your own 'attrs' implementation.

    Add the following lines of code to your JavaScript file:

    jQuery.fn.extend({
        attrs: function (attributeName) {
            var results = [];
            $.each(this, function (i, item) {
                results.push(item.getAttribute(attributeName));
            });
            return results;
        }
    });
    

    Now you can get the list of level values by calling:

    $(".object").attrs("level")
    

提交回复
热议问题