我想在网上制作图片幻灯片,这是我的代码

<head>
<script type="text/javascript">
    var image1 = new Image()
    image1.src = "images/pentagg.jpg"
    var image2 = new Image()
    image2.src = "images/promo.jpg"
</script>
</head>
<body>
<p><img src="images/pentagg.jpg" width="500" height="300" name="slide" /></p>
    <script type="text/javascript">
            function slideit()
            {
                var step=1;
                document.images.slide.src = eval("image"+step+".src")
                if(step<2)
                    step++
                else
                    step=1
                setTimeout("slideit()",2500)
            }
            slideit()
    </script>
</body>

为什么不起作用?我已将想要的图像放在图像文件夹中

最佳答案

  • 将var step = 1设置为全局变量,方法是将其放在函数调用
  • 上方
  • 放入分号

  • 看起来像这样
    <head>
    <script type="text/javascript">
    var image1 = new Image()
    image1.src = "images/pentagg.jpg"
    var image2 = new Image()
    image2.src = "images/promo.jpg"
    </script>
    </head>
    <body>
    <p><img src="images/pentagg.jpg" width="500" height="300" name="slide" /></p>
    <script type="text/javascript">
            var step=1;
            function slideit()
            {
                document.images.slide.src = eval("image"+step+".src");
                if(step<2)
                    step++;
                else
                    step=1;
                setTimeout("slideit()",2500);
            }
            slideit();
    </script>
    </body>
    

    关于javascript - 如何在HTML中创建图片幻灯片?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13860671/

    10-09 22:07