有时候显示不出来?
<div id="ContentTop">test</div><script>
function randomBgImg()   
{     var bg = new Array(8);
bg[0] = 'http://www.famaol.com/image/index02.gif';
bg[1] = 'http://www.famaol.com/image/index03.gif';
bg[2] = 'http://www.famaol.com/image/index04.gif';
bg[3] = 'http://www.famaol.com/image/index05.gif';
bg[4] = 'http://www.famaol.com/image/index06.gif';
bg[5] = 'http://www.famaol.com/image/index07.gif';
bg[6] = 'http://www.famaol.com/image/index08.gif';
bg[7] = 'http://www.famaol.com/image/index09.gif';var index = Math.floor(Math.random() *8);
//alert(document.getElementById("ContentTop"));
document.getElementById("ContentTop").style.backgroundImage='url('+bg[index]+')';

window.onload=function(){  
randomBgImg(); 
};
</script>

解决方案 »

  1.   

    var index = Math.floor(Math.random()*7 + 1);
      

  2.   

    try this
    Math.round(Math.random()*7)+1
      

  3.   

    http://www.famaol.com/image/index05.gif这张图片不存在.
      

  4.   

    晕,你第四个图片有问题,当index = 3时,背景图片是空的。
      

  5.   

    用setInterval都试过了,没问题,只要时间不是特别短的话。不过有时候index=3时好像没图片。
      

  6.   

    不好意思,是我的失误index=3图片没有传上去,这就结贴,谢谢各位。