gpt4 book ai didi

javascript - 尝试使用 javaScript 创建简单的幻灯片,但图像没有改变

转载 作者:行者123 更新时间:2023-12-02 15:54:22 27 4
gpt4 key购买 nike

var getImage = document.getElementById("BCimage");
var imgArray = ["images/1.png","images/2.png","images/3.png","images/4.png"];
var countIndex=0;
function img(){
getImage.setAttribute("src", imgArray[countIndex]);
countIndex++;
if(countIndex>=imgArray.length){
countIndex=0;
}
}
setInterval(img, 5000);

上面是我每 5 秒更改一次图像的简单 js 代码。但图像并没有改变。下面是我的 HTML 代码:

<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1 id="h"><a href="">hi</a></h1>
<div id="divs">
<img alt="image" src="images/1.png" id="BCimage">

</div>


<script type="text/javascript" src="EventsJs.js"></script>

</body>
</html>

最佳答案

检查此链接其在 JSFiddle 中的运行示例,并尝试向我们提供您在 JSFiddle 中与 html 相关的问题,谢谢

var getImage = document.getElementById("BCimage");
var imgArray = ["http://www.si.edu/Content/img/Home/youtube-40x40.png",
"http://www.veryicon.com/icon/256/Emoticon/Black%20Power%20Emoticons/cry%20512x512.png",
"http://www.trynova.org/wp-content/uploads/2013/03/rss-logo-40x40.png"
,
"https://www.teleste.com/sites/default/files/media/fb_icon_40x40.png"];
var countIndex=0;
function img(){
getImage.setAttribute("src", imgArray[countIndex]);
countIndex++;
if(countIndex>=imgArray.length){
countIndex=0;
}
}
setInterval(img, 5000);

Click here to See Your Demo

我在您的示例中添加了在线图片以供您理解。

关于javascript - 尝试使用 javaScript 创建简单的幻灯片,但图像没有改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31667094/

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