有人可以告诉我如何将图像从数组(称为 preLoadArray)一一加载到表格单元格中吗?该表的单元格为 13 x 4。 该函数旨在增加图像 ID 并回调自身以使用短 setTimeout() 添加它们,以延迟该过程并使其动画化。任何帮助,将不胜感激。谢谢。
function showCard(){
//when button is clicked
document.writeln("<table>");
for (m = 0; m < 4; m++) {
document.writeln("<tr>");
for (n = 0; n < 13; n++) {
preLoadImages[n] = new Image();
preLoadImages[n].src = n + '.gif';
document.writeln("<td id=\"\"><img height=\"80\" id=\"" + preLoadImages[(m * 13) + n] + "\" \"></td>");
}
document.writeln("</tr>");
}
document.writeln("</table>");
}
最佳答案
如果您直接指定图像的 src,则无需在任何数组中预加载图像。
这是一个可以帮助您的代码
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script>
function showCard() {
var t = "<table><thead></thead><tbody>";
for (m = 0; m < 4; m++) {
t += "<tr>";
for (n = 0; n < 4; n++) {
t += "<td><img height=\"80\" width=\"80\" src=\"" + ((m * 4) + n) + ".jpg\"/></td>";
}
t += "</tr>";
}
t += "</tbody></table>";
document.getElementById("ll").innerHTML = t;
}
</script>
</head>
<body onload="showCard();">
<div id="ll"></div>
</body>
</html>
关于javascript - 将图像一张一张地添加到表格中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37428481/