简体   繁体   English

如何从Javascript数组中提取图像并将其放入HTML?

[英]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.

相关问题 如何根据用户输入从Google图片中提取图片并将其放置在页面上? - How do I extract images from Google Images based on user input and place them on a page? 如何从Linkedin Javascript API数组中提取phoneNumber - How do I pull phoneNumber from Linkedin Javascript API array 如何获取所有LI UL元素ID值并将它们放置在JavaScript数组中? - How do I get all the LI UL elements ID value and place them in a JavaScript array? 如何从数组中选择随机图像并将其用作 javascript 和 html 中其他图像的来源 - How do I choose a random image from an array and use it as a source for other images in javascript and html 如何从HTML获取ElementsByClassName并将它们推送到JavaScript数组中? 那我怎么能提醒这个阵列呢? - How can I getElementsByClassName from HTML and push them into a JavaScript Array? How can I then alert this array? 如何在javascript / html中调整图像大小 - How do I resize images in javascript/html 如何从javascript中的数组中删除项目? 我想要像拉力之类的东西来做推力的相反 - How can I remove items from an array in javascript ? I want something like a pull to do the opposite of push 如何调用我从 JavaScript 中的数组中提取的函数? - How do I call the function I pull out from an array in JavaScript? 如何从 HTML 表中提取数据,特别是<td id="word"> ,使用 JavaScript? - How do I pull data from a HTML table, specifically <td id="word">, using JavaScript? 我如何使用 javascript 提取 html 内容? - How do i pull in html content using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM