简体   繁体   English

如何调整以下代码以使按钮单击时增加数组增量?

[英]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.

相关问题 如何修改此代码以使行可选? - How can I adapt this code to make the rows selectable? 当我单击制作排序(ab)数组然后再次单击排序(ba)时,如何制作单击按钮 - How can i make a click button when do i click make sort(a-b) array and then click again to sort(b-a) 我如何才能使以下功能仅在右键单击上起作用? - How can i make the following function work on right click only? 如何将以下使用复选框的数据表脚本代码全部调整到我的 asp.net 核心应用程序? - How can I adapt the following datatable script code which uses checkbox all to my asp.net core app? 如何调整JavaScript放大镜以使其单击图像图? - How can I adapt a javascript magnifier to allow it to click on an imagemap? 如何在不缩放的情况下使SVG适应视口大小? - How can I make this SVG adapt to viewport size without scaling? 如何通过单击按钮使6张图像作为gif播放(找不到我的代码中缺少的内容) - How can I make 6 images play as a gif with an on click button (Can't find whats missing in my code) 如何修复以下javascript代码以使其与flowtype一起使用? - How can I fix the following javascript code to make it work with flowtype? 如何使以下代码与 IE 一起使用 - How can I make the following code work with IE 我该如何适应这种高图表代码(闪烁图) - How can i adapt this highcharts code (sparkline charts)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM