1 回答

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
該屬性id
在文檔中必須是唯一的,class
請改為使用。此外,您應(yīng)該在每次迭代中將sec元素附加到當(dāng)前創(chuàng)建的comments元素內(nèi):
for(let n=0; n<5; n++){
? var elm = document.createElement('div');
? elm.setAttribute('class',"comments");
? document.getElementById('elm').appendChild(elm);
? var sec = document.createElement('div');
? sec.setAttribute('class', "sec");
? elm.appendChild(sec); // append the sec element inside the current comments element
}
#elm{
? width: 90vw;
? height: 90vh;
? background: blue;
}
.comments{
? background: brown;
? width: 100%;
? height: 150px;
? border: 2px solid yellow;
}
.sec{
? width: 50px;
? height: 50px;
? background: chartreuse;
? z-index: 1000;
? border: 2px solid darkgreen;
}
<div id="elm"></div>
- 1 回答
- 0 關(guān)注
- 170 瀏覽
添加回答
舉報(bào)