我有 JSON 文件,其中包含:
{
"age": 0,
"id": "motorola-xoom-with-wi-fi",
"imageUrl": "img/phones/motorola-xoom-with-wi-fi.0.jpg",
"name": "Motorola XOOM\u2122 with Wi-Fi",
"snippet": "The Next, Next Generation\r\n\r\nExperience the future with Motorola XOOM with Wi-Fi, the world's first tablet powered by Android 3.0 (Honeycomb)."
},
我需要使用纯 JavaScript 将其显示为“ul”“li”列表
我写了这段代码,但它不起作用:
function createList(){
var arr = JSON.parse(phones);
var out = "<ul>";
for(var i = 0; i < arr.length;i++){
out+="<li>" + arr[i].age + arr.id[i]+
arr[i].imageUrl + arr[i].name + arr[i].snippet + "</li>";
}
out+= "</ul>";
document.getElementById("div1").innerHTML = out;
}
Phones 需要是一个数组,并且您在数组解引用之一上出现语法错误:
var phones = [{
"age": 0,
"id": "motorola-xoom-with-wi-fi",
"imageUrl": "img/phones/motorola-xoom-with-wi-fi.0.jpg",
"name": "Motorola XOOM\u2122 with Wi-Fi",
"snippet": "The Next, Next Generation\r\n\r\nExperience the future with Motorola XOOM with Wi-Fi, the world's first tablet powered by Android 3.0 (Honeycomb)."
}];
function createList(){
var arr = JSON.parse(phones);
var out = "<ul>";
for(var i = 0; i < arr.length;i++){
out+="<li>" + arr[i].age + arr[i].id+
arr[i].imageUrl + arr[i].name + arr[i].snippet + "</li>";
}
out+= "</ul>";
console.log(out);
document.getElementById("div1").innerHTML = out;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)