【问题标题】:get the .json to output on the HTML page using Javascript使用 Javascript 获取要在 HTML 页面上输出的 .json
【发布时间】:2020-08-07 02:25:18
【问题描述】:
【问题讨论】:
标签:
javascript
html
json
image
【解决方案1】:
试试这个:
var jsonObj = {
"items": [
{
"url": "images/image_1.jpg",
"caption": "Image 1 Caption"
},
{
"url": "images/image_2.jpg.jpg",
"caption": "Image 2 Caption"
},
{
"url": "images/image_3.jpg.jpg",
"caption": "Image 3 Caption"
},
{
"url": "images/image_4.jpg.jpg",
"caption": "Image 4 Caption"
}
]
};
function createGallery(array) {
// Create the list element:
var list = document.createElement('ul');
for(var i = 0; i < array.length; i++) {
// Create the list item:
var item = document.createElement('li');
// Set its contents:
item.appendChild(document.createTextNode("Image : "+array[i].url+" Caption: "+array[i].caption));
// Add it to the list:
list.appendChild(item);
}
// Finally, return the constructed list:
return list;
}
// Add the contents of options[0] to #foo:
document.getElementById('gallery').appendChild(createGallery(jsonObj.items));
<div id="gallery"></div>