简单的图像旋转画廊

时间:2014-03-03 09:30:38

标签: javascript

我有这个图像旋转脚本,我无法在任何主要浏览器中工作。 JS所做的是在页面加载上显示随机图像。

这是我的代码:

<script type="text/javascript" src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript">
<!-- Begin
    var theImages = new Array();

    theImages[0] = 'img/rot/forside/FrontReklame1.jpg';
    theImages[1] = 'img/rot/forside/FrontReklame2.jpg';

    var j = 0;
    var p = theImages.length;
    var preBuffer = new Array();
    for (i = 0; i < p; i++){
       preBuffer[i] = new Image()
       preBuffer[i].src = theImages[i]
    };

    var whichImage = Math.round(Math.random()*(p-1));
    function showImage(){
    document.write('<img src="'+theImages[whichImage]+'" alt="Se vores hingste!" />')
    };
    //  End -->
    </script>

我用它来调用图像:

<script type="text/javascript">
<!-- Begin
    showImage();
//  End -->
</script>

此代码不会在网络上显示任何图像,有时当我在本地测试页面时它不会显示?!我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

首先,代码中不需要jquery。 ¿var j的目的是什么?

可能你在Header中运行showImage(),所以你看不到图片。 试试这个:

   function showImage(){
      var selectedImage = preBuffer[whichImage];
      selectedImage.alt = "Se vores hingste!";
      // Using appendChild instead of document.write
      document.body.appendChild(selectedImage);
   };