您好,我需要帮助来访问JavaScript中的对象元素 这是什么错误
var people = [
{
name: "Jake",
age: 20
},
{
name: "John",
age: 23
},
{
name: "Mike",
age: 21
},
]
for (var i=0; i < 3 ;i++)
document.getElementById("myDiv").innerHTML += "Name: " + people[i].name + "Age: "+ people[i].age +"\n";
我只需要将div对象数组的值写入
答案 0 :(得分:1)
您的Java脚本没有任何问题。检查您是否已正确编写myDiv
的所有内容。这是我的示例,效果很好。
请记住放置<br>
标记而不是\n
,而放置" Age: "
而不是"Age: "
。祝你好运!
<!DOCTYPE html>
<html lang="en">
<head></head>
<body>
<div id="myDiv">
<div>
</body>
</html>
<script>
var people = [
{
name: "Jake",
age: 20
},
{
name: "John",
age: 23
},
{
name: "Mike",
age: 21
},
]
for (var i=0; i < 3 ;i++)
document.getElementById("myDiv").innerHTML += "Name: " + people[i].name + " Age: "+ people[i].age + "<br>";
</script>