[英]how can I adapt the following code to make i of the array increment on button click?
with the following code how can I increment the array based on onclick. 使用以下代码如何基于onclick增加数组。 I currently have this on a timer but want it to use the onclick option to change the picture/increment the array 我目前在计时器上有这个,但希望它使用onclick选项来更改图片/增加数组
var i=0;
var images = [];
var time = 1000;
images[0] = 'img1.jpg';
images[1] = 'img2.jpg';
images[2] = 'img3.png';
//change image function
function changeIMG()
{
document.slide.src = images[i];
if(i < images.length - 1)
{
i += 1;
}
else
{
i = 0;
}
setTimeout("changeIMG()", time);
}
window.onload = changeIMG;
First, let's add the button: 首先,让我们添加按钮:
<button id='btn'>Change Image</button>
Now, adding some JavaScript to handle the button's onclick
event: 现在,添加一些JavaScript来处理按钮的onclick
事件:
var btn = document.getElementById('btn');
btn.onclick = changeIMG;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.