我是 JavaScript 新手,我不明白我应该如何在 5 个不同的 gif 图像之间进行更改。任务如下:
第 11 章的 Projects 文件夹包含音乐会系列的五个广告图像,从 Concert1.gif 到 Concert5.gif。创建一个脚本,循环浏览图像并将每个图像显示 5 秒钟。将文档另存为 ConcertAds.html。
这是我到目前为止的代码:
<!DOCTYPE HTML>
<html>
<head>
<title>Concert Ads</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">
/* <![CDATA[ */
var concertads = new Array(5);
concertads[0] = new Image();
concertads[1] = new Image();
concertads[2] = new Image();
concertads[3] = new Image();
concertads[4] = new Image();
concertads[0].src = "concert1.gif";
concertads[1].src = "concert2.gif";
concertads[2].src = "concert3.gif";
concertads[3].src = "concert4.gif";
concertads[4].src = "concert5.gif";
var completeAds;
var currImgIndex = 0;
var maxImgIndex = concertads.length-1;
function concertAd() {
if (completeAds) {
completeAds.src = concertads[0].src;
++currImgIndex;
if (currImgIndex > maxImgIndex) {
currImgIndex = 0;
}
}
}
function initImgCycle(adsID) {
completeAds = document.getElementByID(adsID)
if (completeAds) {
setInterval("cycle()", 5000);
}
}
/* ]]> */
</script>
</head>
<body onload="initImgCycle('adBanner');">
<img src="concert1.gif" id="adBanner" alt="Concert Ads" />
</body>
</html>
我会很感激任何帮助,我真的很困惑。感谢您的时间!