Home  >  Q&A  >  body text

javascript - How to get all attribute names of an img tag node using js?

<img src="" alt="1223" src="hha"/>
<img src=".."/>

Assume that there are many img tags with a structure similar to the above, but the number of attributes of each img tag is unknown (that is, some may only have one src attribute). How to obtain all attribute names of each img tag? (not an attribute value)?

过去多啦不再A梦过去多啦不再A梦2687 days ago1414

reply all(4)I'll reply

  • 阿神

    阿神2017-06-12 09:33:49

    https://developer.mozilla.org...

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-06-12 09:33:49

    [].map.call(document.querySelector(Selector).attributes, (item) => {return item.name})

    reply
    0
  • 为情所困

    为情所困2017-06-12 09:33:49

    // html为 <p class="ha" id="pEle"></p>
    var pEle = document.querySelector('#pEle');  //得到数组
    var attrs = pEle.attributes;  //得到所有属性
    var attrsArray = Array.prototype.slice.call(attrs);      //转换为数组形式
    console.log(attrsArray);    // [class,id]

    reply
    0
  • 某草草

    某草草2017-06-12 09:33:49

    Use ES6 object unwinding:

    [...document.querySelector('selector').attributes]

    reply
    0
  • Cancelreply