尝试用javaScript创建一个简单的幻灯片,但图像没有变化


var getImage = document.getElementById("BCimage");
var imgArray = ["images/1.png","images/2.png","images/3.png","images/4.png"];
var countIndex=0;
function img(){
getImage.setAttribute("src", imgArray[countIndex]);
countIndex++;
if(countIndex>=imgArray.length){
countIndex=0;
}
}
setInterval(img, 5000);

以上是我的简单js代码,每隔5秒改变图像,但图像不移动。下面是我的HTML代码:

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1 id="h"><a href="">hi</a></h1>
<div id="divs">
<img alt="image" src="images/1.png" id="BCimage">
</div>

<script type="text/javascript" src="EventsJs.js"></script>
</body>
</html>

检查这个链接,它在JSFiddle中运行的例子,并尝试给我们你的html在JSFiddle相关的问题,由于

var getImage = document.getElementById("BCimage");
var imgArray = ["http://www.si.edu/Content/img/Home/youtube-40x40.png",
                "http://www.veryicon.com/icon/256/Emoticon/Black%20Power%20Emoticons/cry%20512x512.png",
 "http://www.trynova.org/wp-content/uploads/2013/03/rss-logo-40x40.png"
 ,
 "https://www.teleste.com/sites/default/files/media/fb_icon_40x40.png"];
var countIndex=0;
function img(){
getImage.setAttribute("src", imgArray[countIndex]);
countIndex++;
if(countIndex>=imgArray.length){
countIndex=0;
}
}
setInterval(img, 5000);

点击这里查看您的演示

我把在线图片放在你的例子中,以便你理解。

最新更新