简体   繁体   English

JS DOM 节点:Javascript

[英]JS DOM NODES: Javascript

With the help of a loop, I want to add an element into my list items.在循环的帮助下,我想将一个元素添加到我的列表项中。

Thanks a bunch for any advice!非常感谢您的任何建议!

It's a little hard to tell the context of your problem, but I think using a for...of loop would better meet your needs, as it works with each item of the list individually.很难说出您的问题的上下文,但我认为使用 for...of 循环会更好地满足您的需求,因为它可以单独处理列表中的每个项目。

Try this:尝试这个:

let item = document.getElementsByTagName('li');

for (let i of item){
    i.innerHTML = `${i.innerText} <span class="credits">100 credits</span>`;
}

If you need something that specifically uses an index loop, try this:如果您需要专门使用索引循环的东西,请尝试以下操作:

let item = document.getElementsByTagName('li');

for (let i = 0; i < item.length; i++){
    item[i].innerHTML = `${item[i].innerText} <span class="credits">100 credits</span>`;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM