我有id="correctAnswer"
的5个div。我有一个包含5个元素的数组。如何在5个div中添加这5个元素。我有这个逻辑。
var answers =["David Bowie","AM","Australia","Boneface","Sound City"];
for (i=1; i<=numQues; i++) {
document.getElementById("correctAnswer").innerHTML="correctanswer:"+answers[i-1];
}
<div id="correctAnswer"></div>
</div>
答案 0 :(得分:2)
首先,永远不要使用相同的ID。决不。请改用类。它应该是这样的:
<div class="correctAnswer"></div>
<div class="correctAnswer"></div>
<div class="correctAnswer"></div>
<div class="correctAnswer"></div>
<div class="correctAnswer"></div>
<script>
var answers =["David Bowie","AM","Australia","Boneface","Sound City"];
for (i=1; i<=numQues; i++) {
document.getElementsByClassName("correctAnswer")[i-1].innerHTML="correctanswer:"+answers[i-1];
}
</script>