交换图像时出错 [英] error in swaping images

查看:64
本文介绍了交换图像时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码一个接一个地交换图像,但仅使用< img>中提到的图像;显示标签.图像未按我的要求进行交换.请帮帮我.

I am using the following codes to swap images one after another but only the image mentioned in <img> tag is displayed. The images are not swaping as i want. please help me.

<html>
<head>
<script language="javascript">
(function(){
 var rotator=document.getElementById('rotator');
 var imageDir='';
 var delayInSeconds=2;
 var images=['2.jpg','3.jpg','4.jpg','5.jpg','4.jpg'];

 var num=0;
 var changeImage=function(){
    var len=images.length;
    rotator.src=imageDir+images[num++];
    if(num==len){
        num=0;
        }
    };
    setInterval(changeImage, delayInSeconds * 1000);
    })();
</script>
</head>
<body>
    <img src="4.jpg" alt="rotating image" width="700" height="600" id="rotator">
</body>
</html>

推荐答案

您正在尝试在文档加载之前获取对主闭包中图像元素的引用.

如果将var rotator=...行移动到changeImage函数内部,则该行应该起作用.
You are trying to get a reference to the image element in your main closure, before the document has loaded.

If you move the var rotator=... line inside the changeImage function, it should work.


这篇关于交换图像时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆