[英]How do I pull images from a Javascript array and place them into HTML?
I have the following array of code containing images and their meta Data. 我有以下包含图像及其元数据的代码数组。 ` `
const images = [
{
"src": "Image1.jpeg",
"title": "A two tone wall",
"tags": ["wall", "two tone"]
},
{
"src": "image2.jpeg",
"title": "Cute doggy",
"tags": ["dog", "walkies"]
},
{
"src": "image3.jpeg",
"title": "Natural splendor",
"tags": ["natural", "mountain"]
},
{
"src": "image4.jpeg",
"title": "Man looking at phone",
"tags": ["man", "phone"]
},
{
"src": "image5.jpeg",
"title": "Wavey wave",
"tags": ["water", "wave"]
},
{
"src": "image6.jpeg",
"title": "Mountain range",
"tags": ["mountain", "grass"]
}
]`
and want to place them into a 2 x 3 Grid with attached meta data. 并希望将它们放入带有附加元数据的2 x 3网格中。 At the moment I have tried parsing them using JQUERY into 2 different rows as objects but only text is showing. 目前,我尝试使用JQUERY将它们解析为2个不同的行作为对象,但仅显示文本。
Can anyone help me? 谁能帮我? I'm pretty new to Javascript but understand the principles. 我对Java语言还很陌生,但了解一些原理。
Like this? 像这样?
const cont = document.querySelector(".cont"); const images = [ { "src": "https://picsum.photos/300/200", "title": "A two tone wall", "tags": ["wall", "two tone"] }, { "src": "https://picsum.photos/320/200", "title": "Cute doggy", "tags": ["dog", "walkies"] }, { "src": "https://picsum.photos/330/200", "title": "Natural splendor", "tags": ["natural", "mountain"] }, { "src": "https://picsum.photos/300/210", "title": "Man looking at phone", "tags": ["man", "phone"] }, { "src": "https://picsum.photos/300/220", "title": "Wavey wave", "tags": ["water", "wave"] }, { "src": "https://picsum.photos/300/230", "title": "Mountain range", "tags": ["mountain", "grass"] } ]; images.forEach(function(par){ cont.innerHTML += `<img src="${par.src}" alt="${par.title}">`; });
.cont { display:grid; grid-template-columns:1fr 1fr 1fr; } img { width: 200px; height: 200px; }
<div class="cont"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.