javascript - 是否可以将js文件中的图像动态加载到html文件中?

标签 javascript html json

我有一个 html 文件,其中包含一些图像。但现在我想将 JavaScript 文件(代码如下所示)中的图像加载到我的 html 文件 div 类中,而不总是将其输入到我的 div 类中。有没有办法用 JavaScript、JSON 或其他什么方法来实现这一点?

var swiperExhibitors = ['141726', '141747', '141786', '144994', '145002', '145070', '145072', '151550', '168999', '233192', '545600'];

var swiperImages = ['1437645467.1063_3_o.jpg', '1432890452.7057_3_o.jpg', '1538377347.3443_114_o.jpg', '1495453007.7039_3_o.jpg', '1433941431.3552_3_o.jpg', '1491818250.7553_3_o.jpg', '1364303014.8606_3_o.jpg', 'fa71eb9a8367a0ec85c7b6c05a05b6ef_01_07_o.jpg', '1474012701.9219_3_o.jpg', '1546596386.5453_145_o.jpg', '1554193616.0853_145_o.jpg'];

我想编写一个脚本来为我完成所有工作并在我的 html 文件中动态创建图像

最佳答案

这样您就可以动态创建图像-reference

var swiperImages = ['1437645467.1063_3_o.jpg', '1432890452.7057_3_o.jpg', '1538377347.3443_114_o.jpg', '1495453007.7039_3_o.jpg', '1433941431.3552_3_o.jpg', '1491818250.7553_3_o.jpg', '1364303014.8606_3_o.jpg', 'fa71eb9a8367a0ec85c7b6c05a05b6ef_01_07_o.jpg', '1474012701.9219_3_o.jpg', '1546596386.5453_145_o.jpg', '1554193616.0853_145_o.jpg'];

function myFunction(src) {
  var x = document.createElement("IMG");
  x.setAttribute("src", src);
  x.setAttribute("width", "304");
  x.setAttribute("height", "228");
  x.setAttribute("alt", "The Pulpit Rock");
  document.body.appendChild(x);
}


swiperImages.forEach(el => {
 myFunction(el)

})

关于javascript - 是否可以将js文件中的图像动态加载到html文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56275846/

相关文章:

php - 将数据从本地 MySQL 服务器加载到 Phonegap 应用程序

javascript - 通过 iframe 打印 pdf(跨域)

html - 想要更改当前页面的标签图像

html - 当我将窗口调整为小尺寸时,右边的内容被截断了。

html - css页面最小高度

php - 我如何在 php 中通过 cURL 发布 JSON 对象

java - 如何使用 gson 库形成 json 以循环遍历对象、列表?

javascript - 读取列表中特定元素的数据

javascript - 在下拉菜单中隐藏/显示标签和文本框

javascript - 结果出现时显示顺序