Iterating over element attributes with jQuery

前端 未结 8 937
独厮守ぢ
独厮守ぢ 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: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=`
      
        
        
      
      
        
        
      
    `;
    
    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}`)
    }));

提交回复
热议问题