Get all Attributes from a HTML element with Javascript/jQuery

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

    Use .slice to convert the attributes property to Array

    The attributes property of DOM nodes is a NamedNodeMap, which is an Array-like object.

    An Array-like object is an object which has a length property and whose property names are enumerated, but otherwise has its own methods and does not inherit from Array.prototype

    The slice method can be used to convert Array-like objects to a new Array.

    var elem  = document.querySelector('[name=test]'),
        attrs = Array.prototype.slice.call(elem.attributes);
    
    console.log(attrs);
    See console.

提交回复
热议问题