Get all Attributes from a HTML element with Javascript/jQuery

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

    In javascript:

    var attributes;
    var spans = document.getElementsByTagName("span");
    for(var s in spans){
      if (spans[s].getAttribute('name') === 'test') {
         attributes = spans[s].attributes;
         break;
      }
    }
    

    To access the attributes names and values:

    attributes[0].nodeName
    attributes[0].nodeValue
    

提交回复
热议问题