下面的for循环无法获取数组列表并将其显示在页面上。 鉴于水果和目录列表均不起作用,似乎在进行for循环时犯了一个错误。
// Data for the "HTML Lists" Page
var fruits = [ "Apples","Oranges","Pears","Grapes","Pineapples","Mangos" ];
var directory = [
{type: "file", name: "file1.txt"},
{type: "file", name: "file2.txt"},
{type: "directory", name: "HTML Files", files: [{type: "file", name: "file1.html"},{type: "file", name: "file2.html"}]},
{type: "file", name: "file3.txt"},
{type: "directory", name: "JavaScript Files", files: [{type: "file", name: "file1.js"},{type: "file", name: "file2.js"},{type: "file", name: "file3.js"}]}
];
window.onload=function(){
// list 1
var list1 = document.querySelector("#list1");
var temp1="";
for (var i = 0; i < fruits.length; i++) {
tmep1+="<li>"+fruits[i]+"</li>";
}
list1.innerHTML="<ol>"+temp+"</ol>";
// list 2
var list2 = document.querySelector("#list2");
var list2Text = "";
for (var i = 0; i < directory.length; i++) {
if (directory[i].type == "file") {
list2Text += "<li>" + directory[i].name + "</li>";
} else {
var list = "";
for (var j = 0; j < directory[i].files.length; j++) {
list += "<li>" + directory[i].files[j].name + "</li>";
}
list2Text += "<li>" + directory[i].name + "<ul>" + list + "</ul></li>";
}
}
list2.innerHTML = "<ul>" + list2Text + "</ul>";
}
答案 0 :(得分:3)
这里只有两个错别字:
for (var i = 0; i < fruits.length; i++) {
tmep1+="<li>"+fruits[i]+"</li>";
}
list1.innerHTML="<ol>"+temp+"</ol>";
1-在for
循环内,您需要将tmep1
固定为temp1
2- for
循环之后的行,它是temp1
而不是temp