Iterating over element attributes with jQuery

前端 未结 8 983
独厮守ぢ
独厮守ぢ 2020-11-30 05:38

I know individual attributes can be retrieved with the attr() method, but I\'m trying to iterate over all of the attributes for an element. Fo

8条回答
  •  没有蜡笔的小新
    2020-11-30 06:08

    The best way is to use the node object directly by using its attributes property. The only difference in my solution below compared to others suggesting this method is that i would use .each again instead of a traditional js loop:

    $(xml).find('item').each(function() {
      $.each(this.attributes, function(i, attrib){
         var name = attrib.name;
         var value = attrib.value;
         // do your magic :-)
      });
    });
    

提交回复
热议问题