Get all Attributes from a HTML element with Javascript/jQuery

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

    Element.prototype.getA = function (a) {
            if (a) {
                return this.getAttribute(a);
            } else {
                var o = {};
                for(let a of this.attributes){
                    o[a.name]=a.value;
                }
                return o;
            }
        }
    

    having

    ...
    can use

    mydiv.getA() // {id:"mydiv",a:'1',b:'2'}
    

提交回复
热议问题