多次使用 innerHTML by class
Use innerHTML by class multiple times
我正在尝试插入基于 class 的内容,但它只能工作一次,我怎样才能让它工作多次?
参见示例:.innerHTML 仅运行一次,不会在下一次出现时运行
window.onload = function(){
document.getElementsByClassName("0")[0].innerHTML = `text0`;
document.getElementsByClassName("1")[0].innerHTML = `text1`;
document.getElementsByClassName("2")[0].innerHTML = `text2`;
document.getElementsByClassName("3")[0].innerHTML = `text3`;
}
<div class="x 0"></div>
<div class="x 1"></div>
<div class="x 2"></div>
<div class="x 3"></div>
<div class="x 0"></div>
<div class="x 1"></div>
<div class="x 2"></div>
<div class="x 3"></div>
var elemList = document.getElementsByClassName("0");
for (let i = 0; i < elemList.length; i++) {
elemList[i].innerHTML = "text0";
}
使用 类 1、2 等
document.querySelectorAll('.x').forEach((element, i) => {
element.innerText = `text${i}`
})
我正在尝试插入基于 class 的内容,但它只能工作一次,我怎样才能让它工作多次?
参见示例:.innerHTML 仅运行一次,不会在下一次出现时运行
window.onload = function(){
document.getElementsByClassName("0")[0].innerHTML = `text0`;
document.getElementsByClassName("1")[0].innerHTML = `text1`;
document.getElementsByClassName("2")[0].innerHTML = `text2`;
document.getElementsByClassName("3")[0].innerHTML = `text3`;
}
<div class="x 0"></div>
<div class="x 1"></div>
<div class="x 2"></div>
<div class="x 3"></div>
<div class="x 0"></div>
<div class="x 1"></div>
<div class="x 2"></div>
<div class="x 3"></div>
var elemList = document.getElementsByClassName("0");
for (let i = 0; i < elemList.length; i++) {
elemList[i].innerHTML = "text0";
}
使用 类 1、2 等
document.querySelectorAll('.x').forEach((element, i) => {
element.innerText = `text${i}`
})