我想把不在dom中的图像放在nodeList中

问题描述 投票:0回答:1

我在stackOverflow和其他来源搜索了很多,但我能找到的是从Dom中拉出的nodeLists。我在js文件中创建了自己的元素,我想把数组放在一个能够操作顺序的数组中。

你能在我错的地方帮助我吗?

显然这不起作用。正确创建了图像,但未正确声明nodeList。

const img1 = new Image();
img1.src = "../img/afghanistan.png";
img1.alt = "afghanistan";
img1.width = widthCalculation();
img1.height = heightCalculation();

const img2 = new Image();
img2.src = "../img/angola.png";
img2.alt = "angola";
img2.width = widthCalculation();
img2.height = heightCalculation();

const img3 = new Image();
img3.src = "../img/bahamas.png";
img3.alt = "bahamas";
img3.width = widthCalculation();
img3.height = heightCalculation();

const img4 = new Image();
img4.src = "../img/belgium.png";
img4.alt = "belgium";
img4.width = widthCalculation();
img4.height = heightCalculation();

const img5 = new Image();
img5.src = "../img/bolivia.png";
img5.alt = "bolivia";
img5.width = widthCalculation();
img5.height = heightCalculation();

const img6 = new Image();
img6.src = "../img/kiribati.png";
img6.alt = "kiribati";
img6.width = widthCalculation();
img6.height = heightCalculation();

const img7 = new Image();
img7.src = "../img/mongolia.png";
img7.alt = "mongolia"
img7.width = widthCalculation();
img7.height = heightCalculation();

const img8 = new Image();
img8.src = "../img/panama.png";
img8.alt = "panama";
img8.width = widthCalculation();
img8.height = heightCalculation();

let imgCollection = { const img1, const img2 ,img3 ,img4 ,img5 ,img6 ,img7, img8 };
javascript jquery
1个回答
0
投票

而不是使用以下内容:

const img1 = new Image();

请改用:

var img1 = document.createElement("img");

后者将创建一个元素,您可以配置该元素,然后将其附加到您的集合中。

var images = [];

for (var i = 1; i <= 5; i++) {
  var img = document.createElement("img");
  img.src = `img${i}.png`;
  images.push(img);
}

console.log(images);
© www.soinside.com 2019 - 2024. All rights reserved.