Iterating over element attributes with jQuery

前端 未结 8 813
独厮守ぢ
独厮守ぢ 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 05:48

    How about?

    $(xml).find('item').each(function() {
      var attributes = $(this)[0].attributes;
      for (attribute in attributes) {
        // Do something with each attribute...
      }
    });
    
    0 讨论(0)
  • 2020-11-30 05:57

    Pure JS First, your input xml is not valid, but you can fix it by close subitem tags by /

    let items = (new DOMParser()).parseFromString(xml,"text/xml").querySelectorAll('item');
    
    items .forEach( (item,i)=> Object.values(item.attributes).forEach(a => {
      // your code
      console.log(`Item ${i}. attr ${a.name} = ${a.value}`)
    }));
    

    let xml=`<items>
      <item id="id123" name="Fizz" value="Buzz" type="xyz">
        <subitem name="foo" />
        <subitem name="bar" />
      </item>
      <item id="id456" name="Bizz" value="Bazz" type="abc">
        <subitem name="meh" />
        <subitem name="hem" />
      </item>
    </items>`;
    
    let items  = (new DOMParser()).parseFromString(xml,"text/xml").querySelectorAll('item');
    
    items .forEach( (item,i)=> Object.values(item.attributes).forEach(a => {
      // your code
      console.log(`Item ${i}. attr ${a.name} = ${a.value}`)
    }));

    0 讨论(0)
  • 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 :-)
      });
    });
    
    0 讨论(0)
  • 2020-11-30 06:09

    Could you get the DOM element from the jQuery wrapper using the get() function, and then iterate the DOM attributes?

    var node = $(myStuff).get(0);
    if (node.attributes.length) {
    
        for (var length = attrs.length, i = 0; i < length; i++) {
            if (attrs[i].specified) {
    
            }
        }
    }
    

    For some much more robust DOM attribute handling, check this blog post.

    0 讨论(0)
  • 2020-11-30 06:10

    it seems you have to use plain old vanilla javascript:

    for (var i = 0; i < elem.attributes.length; i++) {
        var attrib = elem.attributes[i];
        if (attrib.specified == true) {
            console.log(attrib.name + " = " + attrib.value);
        }
    }
    

    How to iterate through all attributes in an HTML element?

    0 讨论(0)
  • 2020-11-30 06:11

    created this generic function that allows to look-in attributes as well as innearHtml:

    function findByAll(toLookFor, lookInText) { 
        return $('*').filter(function() { 
            return Array.prototype.some.call(this.attributes, function(attr) {
                return attr.value.indexOf(toLookFor) >= 0; 
            }) || (lookInText && $(this).text().indexOf(toLookFor) >= 0); 
        }); 
    }
    
    0 讨论(0)
提交回复
热议问题