简体   繁体   English

我想将不在dom上的图像放在nodeList中

[英]I want to put image who are not on the dom in a nodeList

I have googled a lot in stackOverflow and other sources but all I can find are nodeLists pulled from the Dom. 我在stackOverflow和其他来源中搜索了很多,但是我所能找到的只是从Dom中提取的nodeLists。 I created my own elements in the js file and i want to put the in an array to be able to manipulate the order. 我在js文件中创建了自己的元素,我想将其放入数组中以便能够处理订单。

Can you help me where I'm wrong. 如果我错了,您能帮我吗?

Obviously this didn't work. 显然,这没有用。 The images are correctly created but the nodeList is not rightly declared. 映像已正确创建,但未正确声明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 };

Instead of using the following: 而不是使用以下内容:

const img1 = new Image();

Use this instead: 使用此代替:

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

The latter will create an element that you can configure and then append to your collection. 后者将创建一个您可以配置的元素,然后将其追加到您的集合中。

 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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM