应该很简单,但我只获得Jimmy板球输出,我期待li标签中的所有名字。谢谢你的帮助。
<ul id="members"></ul>
<script>
var teammembers = [
{"name":"John Doe", "profile":"/img/profile/user1.jpg", "position":"President", "email":"email@example.com", "phone":"242-abcd"},
{"name":"James Bond", "profile":"/img/profile/user2.jpg", "position":"Vice President", "email":"007@example.com", "phone":"242-0007"},
{"name":"Jimmy Cricket", "profile":"/img/profile/user3.jpg", "position":"Vice Cricket", "email":"cricket@example.com", "phone":"242-wxyz"}
];
for (var i = 0; i < teammembers.length; i++) {
document.getElementById("members").innerHTML = "<li>" + teammembers[i].name; + "</li>"
}
</script>
答案 0 :(得分:3)
这里的=
符号在每次迭代时替换innerHTML。因此,您可以在此处看到数组的最后一个值。
将其转换为+=
。像这样,
document.getElementById("members").innerHTML += "<li>" + teammembers[i].name; + "</li>"
+=
会追加它。
答案 1 :(得分:1)
您需要附加到innerHTML
,而不是设置它。而不是:
document.getElementById("members").innerHTML = "<li>" + teammembers[i].name; + "</li>"
使用此功能(更改是使用=
改为使用+=
):
document.getElementById("members").innerHTML += "<li>" + teammembers[i].name; + "</li>"