我正在尝试使用循环使用来自 5 个不同 div 元素的 HTML 填充数组的内容。我无法让 for 循环正常工作。有任何想法吗?
的HTML:
<div id="person0">John</div>
<div id="person1">Kathleen</div>
<div id="person2">Cynthia</div>
<div id="person3">Eric</div>
<div id="person4">Jay</div>
<div style="width: 600px; margin: auto; padding: 15px; border: 1px solid gray; font-family: sans-serif;">
<h1>The People</h1>
<div id="result"></div>
</div>
Javascript:
function pageLoaded(){
var list = "<ul>";
var myArray = new Array();
for(i=0; i<6; i++){
var person = "person";
var personNumber = "" + i.toString();
var divId = person.concat(personNumber);
myArray[i] = document.getElementById(divId).innerHTML;
list +="<li>"+myArray[i]+"</li>";
}
list +="</ul>";
document.getElementById("result").innerHTML=list;
}