我正在学习JS教程,但我遇到了一个问题:我的浏览器中没有显示该列表。
我检查了脚本部分,但一切都还好。我在代码中找不到错误。
我页面浏览器上的输出应该与此列表类似:
*facebook:...
*instagram:...
脚本:
var info= {
"full_name": "tim berner ",
"tile": "staff author",
"links": [
{"facebook": "https://web.facebook.com/"},
{"instagram": "https://www.instagram.com/"}
]
};
var output = '';
for (var i = 0; i <= info.length; i++){
for (key in info.links[i]){
if (info.links[i].hasOwnProperty(key)){
output += '<li>' +
'<a href= "' + info.links[i] [key] +
' ">' + key + '</a> ' +
'</li>' ;
} //hasOwnProperty
} //for each object
} //for each array element
var update = document.getElementById('links');
update.innerHTML = output;
答案 0 :(得分:0)
您在for语句条件中缺少.links
。请参阅下面的JavaScript代码:
var info = {
"full_name": "tim berner ",
"tile": "staff author",
"links": [
{ "facebook": "https://web.facebook.com/" },
{ "instagram": "https://www.instagram.com/" }
]
};
var output = '';
for (var i = 0; i <= info.links.length; i++) {
for (key in info.links[i]) {
if (info.links[i].hasOwnProperty(key)) {
output += '<li>' +
'<a href= "' + info.links[i][key] +
' ">' + key + '</a> ' +
'</li>';
}//hasOwnProperty
}//for each object
}//for each array element
var update = document.getElementById('links');
update.innerHTML = output;