Get all Attributes from a HTML element with Javascript/jQuery

后端 未结 17 2114
时光说笑
时光说笑 2020-11-22 05:07

I want to put all attributes in a Html element into an array: like i have a jQuery Object, whichs html looks like this:



        
17条回答
  •  生来不讨喜
    2020-11-22 05:28

    You can use this simple plugin as $('#some_id').getAttributes();

    (function($) {
        $.fn.getAttributes = function() {
            var attributes = {}; 
    
            if( this.length ) {
                $.each( this[0].attributes, function( index, attr ) {
                    attributes[ attr.name ] = attr.value;
                } ); 
            }
    
            return attributes;
        };
    })(jQuery);
    

提交回复
热议问题