gpt4 book ai didi

javascript - 在 Javascript 中使用循环创建和加载图像

转载 作者:行者123 更新时间:2023-11-30 08:52:16 25 4
gpt4 key购买 nike

我很抱歉,但我是一个严重的新手。

有人可以告诉我如何使用循环加载图像吗?

即重写以下类型的代码以使用循环来自动执行该过程。

function loadimages() {
pic00 = new Image;
pic00.src = "images/IMG_0019.jpg";

pic01 = new Image;
pic01.src = "images/IMG_0020.jpg";

pic02 = new Image;
pic02.src = "images/IMG_0021.jpg";

pic03 = new Image;
pic03.src = "images/IMG_0022.jpg";

pictures = new Array(4);
pictures[0] = pic00;
pictures[1] = pic01;
pictures[2] = pic02;
pictures[3] = pic03;
}

我看过许多描述类似事情的帖子,但恐怕我太笨了,无法理解它们。任何帮助表示赞赏。

问候

最佳答案

这样做会:

var URLs = [
"http://placehold.it/128x128.png/f00/400?text=Red",
"http://placehold.it/128x128.png/0f0/040?text=Green",
"http://placehold.it/128x128.png/00f/004?text=Blue",
"http://placehold.it/128x128.png/ff0/440?text=Yellow"
];

var imgs = URLs.map(function(URL) {
var img = new Image();
img.src = URL;
document.body.appendChild(img);
return img;
});

Demo

关于javascript - 在 Javascript 中使用循环创建和加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16825964/

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