gpt4 book ai didi

javascript - html/javascript 幻灯片无法正常工作

转载 作者:行者123 更新时间:2023-11-28 19:02:57 25 4
gpt4 key购买 nike

我想在我的网页中播放幻灯片,但我的代码无法正常工作。这是我的 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>

为什么它没有按预期工作?

最佳答案

工作 fiddle :https://jsfiddle.net/uff8nydv/

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();

还要确保您识别 <img>id="slideshow"

<body>
<img src="../images/slideshow/1.jpg" width="500" height="300" id="slideshow"/>
</body>

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com