我想将不在节点符中的DOM放在DOM上的图像



我在stackoverflow和其他来源中搜索了很多谷歌搜索,但我所能找到的只是从DOM中提取的结节性。我在JS文件中创建了自己的元素,我想将其放入数组中以操纵订单。

你能帮我我错了吗?

显然这不起作用。这些图像是正确创建的,但没有正确声明。

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

而不是使用以下内容:

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

最新更新