Get all Attributes from a HTML element with Javascript/jQuery

后端 未结 17 1993
时光说笑
时光说笑 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:36

    Very simple. You just need to loop over the attributes element and push their nodeValues into an array:

    let att = document.getElementById('id');
    
    let arr = Array();
    
    for (let i = 0; i < att.attributes.length; i++) {
        arr.push(att.attributes[i].nodeValue);
    }
    

    If want the name of the attribute you can replace 'nodeValue' for 'nodeName'.

    let att = document.getElementById('id');
    
    let arr = Array();
    
    for (let i = 0; i < att.attributes.length; i++) {
        arr.push(att.attributes[i].nodeName);
    }
    

提交回复
热议问题