1

这是我的作业网站的 JavaScript 部分 - 它应该显示一个约会广告,并每隔一段时间在屏幕上移动它,并在每个时间间隔加载一个新图像

它会随机水平移动,但不会垂直移动。这个问题发生在 Firefox 中 - 在 IE 中没有问题。

但是在 IE 中它不会遍历图像,但在 Firefox 中会。

谁能告诉我发生了什么事?

<script type="text/javascript">
    var rotatingAd = new Array(3);
    var curPosition = 1;
    var screenWidth = 0;
    var screenHeight = 0;

    for(var i = 0; i < 4; ++i) 
    {
        rotatingAd[i] = new Image();

        rotatingAd[i].src = "AdDate" + i + ".gif";

    }
    function openOrderForm()
    {
        window.open("order.html");
    }



    function moveAd(){
    var leftPos = getRandomLeft();
    var topPos = getRandomTop();
    document.getElementById("AdDate").style.top = topPos; + "px";//this is where firefox says the css error is 

          document.getElementById("AdDate").style.left = leftPos + "px";
    if(curPosition == 3)
    {
        curPosition = 0;
    }
    document.AdDatePic.src = rotatingAd[curPosition].src;//this is where IE gets its null error from    
    curPosition++;

}

    function start(){
  screenWidth = document.body.clientWidth;
  screenHeight= document.body.clientHeight;
  //alert("width="+screenWidth + "height="+screenHeight)
 //setTimeout("moveAd()", 3000);
  setInterval("moveAd()", 2000);
}

    function getRandomLeft(){
     var lpos;
     lpos = Math.floor(Math.random()*(screenWidth -400));
     return lpos;
    }

    function getRandomTop(){
     tpos = Math.floor(Math.random()*(screenHeight - 135));
      return tpos;
    }
</script>
<body onload="start();">
<span id="AdDate" style="position:absolute; left:300px; top:300px;" >
  <img id="AdDatePic" src="AdDate0.gif" alt="picture of gopher"   />
</span>
</body>
4

1 回答 1

4

看起来您的 IE 问题与您尝试选择图片标签的方式有关

将您的代码更改为

document.getElementById("AdDatePic").src

document.AdDatePic.src

应该修复IE问题

firefox问题来自分号放置

topPos + "px;"

应该解决这个问题

于 2012-09-18T18:42:32.817 回答