📜  vanilla js为每个元素添加属性 - Javascript代码示例

📅  最后修改于: 2022-03-11 15:04:18.483000             🧑  作者: Mango

代码示例1
const linksArray = Array.from(document.querySelectorAll('section a')); // we use Array.from to transform the NodeList from querySelectorAll to an array. Needs to IE11

linksArray.forEach(linkEl => { // we search for every span and em inside the link
 linkEl.setAttribute("list-span", linkEl.querySelector('span').textContent);
 linkEl.setAttribute("list-em", linkEl.querySelector('em').textContent);
});