这就是我想我想做的事情,但我无法绕着正确的方式去做。
所以我将一堆图像加载到变量中(想想数字时钟)......
var digit0 = "images/d0.png";
var digit1 = "images/d1.png";
var digit2 = "images/d2.png";
var digit3 = "images/d3.png";
var digit4 = "images/d4.png";
var digit5 = "images/d5.png";
var digit6 = "images/d6.png";
var digit7 = "images/d7.png";
var digit8 = "images/d8.png";
var digit9 = "images/d9.png";
然后在循环中我尝试将这些图像加载到div ...
//SKIP A BUNCH OF TIMER CODE THAT WORKS AND GIVES ME "seconds".
var secondDigit = "digit"+(seconds.toString()).substr(1,1);
//Trying to make a string that refers to the matching variable above.
$("#d1").html('<img src="'+digit0+'" />'); //-- THIS ONE WORKS
$("#d2").html('<img src="'+secondDigit+'" />'); //-- THIS ONE DOESN'T
编辑:那么使用这种方法,它每次都会点击服务器吗?如果是这样,那不是我想要的。我想保存一次图像,这样我就可以反复使用它们,而不必每次都点击服务器。
谢谢!
答案 0 :(得分:1)
您的图片将由浏览器缓存,因此无需担心。
使用实际数组而不是变量数组(:P):
var digits [
"images/d0.png",
"images/d1.png",
"images/d2.png",
"images/d3.png",
"images/d4.png",
"images/d5.png",
"images/d6.png",
"images/d7.png",
"images/d8.png",
"images/d9.png"
];
var secondDigit = seconds.toString().substr(1,1);
$("#d2").html('<img src="' + digits[secondDigit] + '" />');