我目前正在创建一个网页照片库。我有很多图像我想在页面加载后用 Javascript 预加载。与其在我的数组中拥有一个非常非常长的 HTML 链接列表,是否可以利用for
循环?请看我下面的代码。任何关于我在 for 循环中做错的有用见解都将非常感激!谢谢!!
<script type="text/javascript">
function preloader() {
var images = new Array()
function preload() {
for (i = 0; i < preload.arguments.length; i++) {
images[i] = new Image()
images[i].src = preload.arguments[i]
}
}
preload(
var i=1;
"http://example.com/images/gallery/elephants-" + for (i=1;i<=5;i++) {document.write(i);} + ".jpg",
"http://example.com/images/gallery/penguins-" + for (i=1;i<=2;i++) {document.write(i);} + ".png"
)
}
function addLoadEvent(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() {
if (oldonload) {
oldonload();
}
func();
}
}
}
addLoadEvent(preloader);
</script>
我遇到问题的部分是该部分中的for
循环preload()
。该preload()
部分应输出/执行此操作:
preload(
"http://example.com/images/gallery/elephants-1.jpg",
"http://example.com/images/gallery/elephants-2.jpg",
"http://example.com/images/gallery/elephants-3.jpg",
"http://example.com/images/gallery/elephants-4.jpg",
"http://example.com/images/gallery/elephants-5.jpg",
"http://example.com/images/gallery/penguins-1.png",
"http://example.com/images/gallery/penguins-2.png"
)