【发布时间】:2019-07-07 07:47:54
【问题描述】:
我正在尝试使用 javascript 制作带有下载按钮的图像网格,以将它们放入 html 中。
我尝试过 insertBefore。
var list = ["https://via.placeholder.com/50", "https://via.placeholder.com/75", "https://via.placeholder.com/100", "https://via.placeholder.com/125"];
var template, item, item1, item2, fin, i, target, ta;
template = document.getElementById("item");
item = template.content.getElementById("grid-itm");
item1 = template.content.getElementById("imgtit");
item2 = template.content.getElementById("linktit");
target = document.getElementById("gc");
for (i = 0; i < list.length; i++) {
target = document.getElementById("gc");
ta = list[i];
item1.src = ta;
item2.href = ta;
item.appendChild(item1);
item.appendChild(item2);
target.appendChild(item);
}
<template id="item">
<div class="grid-item" id="grid-itm">
</div>
<img src="" id="imgtit">
<a href="" id="linktit"><p>Download</p></a>
</template>
<div class="grid-container" id="gc"></div>
.grid-container 中应该是我所有的图片,但它只是列表中的最后一张。
【问题讨论】:
标签: javascript html dom