0
JavaScript로 배열을 텍스트 영역에 인쇄하려고합니다. 내 현재 코드는 네 가지 대신 첫 번째 요소 만 인쇄합니다. 왜 그런지 모르겠습니다. 일반 for 루프도 사용해 보았지만 차이는 없습니다. 때문에 당신이 무슨 말을이 라인 그래서배열을 JavaScript의 텍스트 영역에 인쇄하십시오.
document.getElementById("box").innerHTML = string;
이다
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var listArray = function()
{
var people = ["Ben", "Joel", "Mary", "Tina"];
var scores = [88, 98, 100, 78];
for (var key in people)
{
var obj = people[key];
var num = scores[key];
var string = obj + ", " + num + "\n";
document.getElementById("box").innerHTML = string;
}
}
window.onload = function()
{
document.getElementById("show_score").onclick = showHighScore;
document.getElementById("list_array").onclick = listArray;
}
</script>
</head>
<body>
<form id="high_score" name="high_score" action="highScore.html" method="get">
<label>Results</label>
<br>
<textarea cols="50" rows="4" id="box"></textarea>
<br>
<input type="button" value="List Array" id="list_array" onclick="listArray">
<br>
<input type="button" value="Show Best Score" id="show_score" onclick="showHighScore">
</form>
</body>
</html>
'.innerHTML = ...'이 (가) 겹쳐 쓰기 만하면됩니다. '.innerHTML + = ...'이 추가됩니다. 또한 'showHighScore'가 정의되어 있지 않음에 유의하십시오. –