gpt4 book ai didi

javascript - 如何在 Javascript 中将文本与图像相关联?

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:01 24 4
gpt4 key购买 nike

我有一个简单的问题要问。我在这里创建了一个可用的随机生成器:

var randPics = document.querySelector("#randPics");
var getPics = document.querySelector(".getPics");


getPics.addEventListener("click", function(){
//array to store images for the random image generator
var picsGallery = new Array();
picsGallery = ["https://static2.jetpens.com/images/a/000/026/26648.jpg?mark64=aHR0cDovL3d3dy5qZXRwZW5zLmNvbS9pbWFnZXMvYXNzZXRzL3dhdGVybWFyay5wbmc&markalign64=dG9wLHJpZ2h0&markscale=19&s=938428f6eca690069a86f66d0754444b",
"http://assets.sajiansedap.com/media/article_image/cover/large/25505-cara-memilih-nanas-yang-matang.jpg",
"https://cdn.shopify.com/s/files/1/1030/8703/products/epal-hijau-green-apple-each-sebiji_1024x1024.jpg?v=1487817043"]

//generate random no to select the random images
var rnd = Math.floor(Math.random() * picsGallery.length);
//change the pics locations of the source
randPics.src=picsGallery[rnd]
});
#randPics{
width: 300px;
height: 300px;
align-content: center;
}
<body>
<p>Display a random image each time the buttons is clicked!</p>
<p> You get a <span id="text"></span> </p>

<button class="getPics"> Click ! </button>
<br>
<img id="randPics" src="https://d30y9cdsu7xlg0.cloudfront.net/png/45447-200.png">

</body>

当用户点击按钮时,图片源会随机选择数组中的一张图片。但是,我有一点问题。如何将文本关联到图像?例如,如果用户单击按钮并获得钢笔的图像,文本

You get a

应该改为

You get a pen.

感谢您的帮助!

最佳答案

首先,您需要将文本存储在某处,然后您必须有一种方法将文本与图像相关联,为此您可以将与图像相关联的文本存储在一个对象中,然后拥有一个此类对象的数组,而不仅仅是图片 url 数组。此外,您无需在事件处理程序函数内声明数组,您可以定义一次,然后在函数内使用它,因为它将在范围内。像这样:

var randPics = document.querySelector("#randPics");
var getPics = document.querySelector(".getPics");
var textElem = document.querySelector("#text");

//array to store images for the random image generator
var picsGallery = picsGallery = [{img: "https://static2.jetpens.com/images/a/000/026/26648.jpg?mark64=aHR0cDovL3d3dy5qZXRwZW5zLmNvbS9pbWFnZXMvYXNzZXRzL3dhdGVybWFyay5wbmc&markalign64=dG9wLHJpZ2h0&markscale=19&s=938428f6eca690069a86f66d0754444b", text:'pen'},
{img: "http://assets.sajiansedap.com/media/article_image/cover/large/25505-cara-memilih-nanas-yang-matang.jpg", text:'pineapple'},
{img: "https://cdn.shopify.com/s/files/1/1030/8703/products/epal-hijau-green-apple-each-sebiji_1024x1024.jpg?v=1487817043", text:'apple'}];

getPics.addEventListener("click", function(){

//generate random no to select the random images
var rnd = Math.floor(Math.random() * picsGallery.length);
//change the pics locations of the source
randPics.src=picsGallery[rnd].img;
text.innerHTML = picsGallery[rnd].text;
});
#randPics{
width: 300px;
height: 300px;
align-content: center;
}
<body>
<p>Display a random image each time the buttons is clicked!</p>
<p> You get a <span id="text"></span> </p>

<button class="getPics"> Click ! </button>
<br>
<img id="randPics" src="https://d30y9cdsu7xlg0.cloudfront.net/png/45447-200.png">

</body>

关于javascript - 如何在 Javascript 中将文本与图像相关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45402134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com