0

我正在使用以下代码从文件夹中动态选择图像,然后在幻灯片中显示它们。但是此代码仅选择第一张图像...请指导我错误在哪里...

PHP文件是:命名:test.php

<?php
 header("content-type: application/x-javascript");
    function returnimages($dirname=".") {
        $files = array();
        $curimage = 0;
         //valid image extensions
        $pattern="(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)";
        if($handle = opendir($dirname)) {
            while(false !== ($file = readdir($handle))) {
                if(eregi($pattern, $file)){
                    echo 'galleryarray[' . $curimage . '] = "' . $file . '";';
                    $curimage++;
                }
            }
           closedir($handle);
        }
        return($files);
    }
    //Define array in JavaScript returnimages()
    //Output the array elements containing the image file names
    echo 'var galleryarray = new Array();';
?>

html代码是:

<html>
    <head>
        <title></title>
        <script src="test.php"></script>
        <script type="text/javascript">
            var galleryarray = returnimages();
            var curimg = 0;
            function rotateimages(){
                //  var imagesDirectory = "pics/" + galleryarray[curimg];
                var imagesDirectory =galleryarray[curimg];
                document.getElementById("slideshow").setAttribute("src", imagesDirectory)
                curimg = (curimg < galleryarray.length - 1) ? curimg + 1 : 0
            }
            window.onload = function(){
                setInterval("rotateimages()", 2500)
            }
        </script>
    </head>
    <body>
        <img width="468" height="312" id="slideshow" src="Slide1.jpg">
    </body>
</html>
4

1 回答 1

3

您实际上是在尝试通过 javaScript调用php函数。returnimages()无论如何这是不可能的。

于 2012-08-30T09:26:11.083 回答