图像交替功能交替显示错误的图像

时间:2015-07-07 04:00:41

标签: javascript html

我是HTML,JavaScript,CSS的新手,正在学习Web Design课程。我需要制作横幅广告,在两个不同的图片(banner1.jpgbanner2.jpg)之间切换。我遇到的问题是,当我的网站加载时,我会在banner1.jpg上方显示<h2>,并在court.jpg下方显示court.jpg。然后它将banner2.jpg更改为banner1.jpg,然后更改为banner1.jpg,然后交替返回&amp;在两个横幅之间。 <h2>上方的<script type="text/javascript"> /* <![CDATA[ */ var curImage="banner1"; function bannerAd() { if (curImage == "banner2") { document.images[1].src = "images/banner1.jpg"; curImage = "banner1"; } else { document.images[1].src = "images/banner2.jpg"; curImage = "banner2"; } } /* ]]> */ </script> </head> <body onload="var changeImages=setInterval('bannerAd()', 2000);"> <header> <h1>Basketball Almanac</h1> </header> <section class="main"> <img src="images/banner1.jpg" alt="Banner image" /> <h2>Basketball Analysis</h2> <p><span class="companyname">Basketball Almanac</span> is your one-stop site for in-depth basketball analysis and statistics.</p> <img class="main" src="images/court.jpg" alt="NBA Court" /> 保持不变。这是相关的代码:

banner1.jpg

如何才能使public abstract class Language { // Field to identify the language by. public string Code {get; private set;} // Contructor that takes language code as parameter public Language(string code) { Code = code; } // Add the strings you want translating. // Making them abstract means classes that inherit from // this class must provide their own implementation of these strings. public abstract string Hello {get;} public abstract string Goodbye {get;} } 更改?在此先感谢您的帮助,并告诉我是否有助于在此处添加更多代码。

3 个答案:

答案 0 :(得分:2)

简单,在javascript中,数组基于0,因此frist对象为[0]

所以你想要

var curImage="banner1";

function bannerAd() {
 if (curImage == "banner2") {
 document.images[0].src = "images/banner1.jpg";
 curImage = "banner1";
 }
 else {
 document.images[0].src = "images/banner2.jpg";
 curImage = "banner2";
 }
}

或简化一点

var curImage="images/banner2.jpg";

function bannerAd() {
 if (curImage == "images/banner1.jpg") {
     curImage= "images/banner2.jpg";
 }
 else {
     curImage = "images/banner1.jpg";
 }

 document.images[0].src = curImage;
}

答案 1 :(得分:1)

&#13;
&#13;
var curImage="images/banner2.jpg";

function bannerAd() {
 if (curImage == "images/banner1.jpg") {
     curImage= "images/banner2.jpg";
 }
 else {
     curImage = "images/banner1.jpg";
 }

 document.images[0].src = curImage;
}
&#13;
&#13;
&#13;

答案 2 :(得分:0)

var curImage="banner1";

function bannerAd(n) {
 if (curImage == "banner2") {
 document.images[n].src = "images/banner1.jpg";
 curImage = "banner1";
 }
 else {
 document.images[n].src = "images/banner2.jpg";
 curImage = "banner2";
 }
}
//bannerAd(1)
  <body onload="var changeImages=setInterval('bannerAd(0)', 2000);">
    <header>
        <h1>Basketball Almanac</h1>
    </header>
<section class="main"> 
    <img src="images/banner1.jpg" alt="Banner image" />
    <h2>Basketball Analysis</h2>
    <p><span class="companyname">Basketball Almanac</span> is your one-stop site for in-depth basketball analysis and statistics.</p>
    <img class="main" src="images/court.jpg" alt="NBA Court" />