我想在网页中放映幻灯片,但是我的代码无法正常工作。
这是我的HTML:

<body>
<script type="text/javascript">
var slideNumber = 1;
function slide() {
    document.getElementById("slideshow").src = "../images/slideshow/" + slideNumber + ".jpg";
    slideNumber++;
    if (slideNumber == 5) {
        slideNumber = 1;
    }
    setTimeout("slide()",2500);
}
slide();
</script>
<img id="slideshow" src="../images/slideshow/1.jpg" width="500" height="300"/>
</body>


为什么它不能按预期工作?

最佳答案

工作提琴:
还要确保将<img>标识为id="slideshow"

var slideNumber = 1;

function slide() {

    document.getElementById("slideshow").src = "../images/slideshow/" + slideNumber + ".jpg";
    console.log("../images/slideshow/" + slideNumber + ".jpg");

    slideNumber++;

    if (slideNumber == 5) {
        slideNumber = 1;
    }

    setTimeout(slide,500);
}

slide();

关于javascript - html/javascript幻灯片不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32208368/

10-09 17:59