Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript幻灯片的图像数组未出现_Javascript_Html_Arrays_Image_Slideshow - Fatal编程技术网

javascript幻灯片的图像数组未出现

javascript幻灯片的图像数组未出现,javascript,html,arrays,image,slideshow,Javascript,Html,Arrays,Image,Slideshow,我正在用javascript为一个类分配制作一个幻灯片,我让幻灯片工作,但它没有显示图像。我可以看到图像图标发生了变化,但实际图像没有显示出来 <script type="text/javascript"> //put images in array var pics = new Array(); pics[0] = new Image(); pics[0].src = "images/forest.jpg";

我正在用javascript为一个类分配制作一个幻灯片,我让幻灯片工作,但它没有显示图像。我可以看到图像图标发生了变化,但实际图像没有显示出来

<script type="text/javascript">
        //put images in array
        var pics = new Array();
        pics[0] = new Image();
        pics[0].src = "images/forest.jpg";
        pics[1] = new Image();
        pics[1].src = "images/mountains.jpg";
        pics[2] = new Image();
        pics[2].src = "images/nature.jpg";
        pics[3] = new Image();
        pics[3].src = "images/snowtops.jpg";
        var index = 0; //start point
        var piclength = pics.length - 1;

        function slideshow() {
            document.slide.src = pics[index];
            if (index < piclength) {
                index++;
            }
            else {
                index = 0;
            }
        }

        function slide() {
            setInterval(slideshow, 3000);
        }
    </script>

<body onload="slide()">
    <h1>Nature Photography</h1>
    <main>
        <section>
            <p>I am an enthusiastic about nature photography. Here is a slideshow of my 
works.</p>
            <aside> <img id="myImage" src="images/forest.jpg" name="slide" width="95%"> 
</aside>

//将图像放入数组中
var pics=新数组();
pics[0]=新图像();
pics[0].src=“images/forest.jpg”;
pics[1]=新图像();
pics[1].src=“images/mountains.jpg”;
pics[2]=新图像();
pics[2].src=“images/nature.jpg”;
pics[3]=新图像();
pics[3].src=“images/snowtops.jpg”;
var指数=0//起点
var piclength=pics.length-1;
函数幻灯片(){
document.slide.src=pics[index];
如果(索引

首先,我会将脚本标记放在HTML之后。这将允许您缓存DOM元素,而无需等待触发“DOMContentLoaded”事件或“load”(窗口)事件。
其次,应该缓存“myImage”元素。类似于
const slider=document.getElementById('myImage')

第三,检查你的控制台。也许你的图像URL是错误的?并确保您的HTML是有效的。从您发布的内容来看,您缺少了很多东西(doctype、html/head标记、您没有关闭body标记和类似标记)

您需要阅读DOM操作的基本示例。HTML不是这样工作的。你需要使用appendchild或其他什么工具来做回答,我实际上已经弄明白了为什么图像没有显示出来。document.slide.src=pics[index];实际上应该是document.slide.src=pics[index].src;