Isa*_*son 2 javascript arrays loops
我试图基本上创建一个自动收报机,它显示一次从数组中取出的单个字符串项.基本上我只是希望它显示一个项目,然后转换到下一个,我的Javascript技能是大规模的基本(Jquery可能会更好.)
这是我的代码:
var title= ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
for (var i=0; i<title.length; i++){
document.write(title[i]);
}
Run Code Online (Sandbox Code Playgroud)
我需要添加什么?
谢谢你!
首先了解document.getElementById和setInterval.
HTML:
<span id="fruit"></span>
Run Code Online (Sandbox Code Playgroud)
使用Javascript:
var title = ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
var i = 0; // the index of the current item to show
setInterval(function() { // setInterval makes it run repeatedly
document
.getElementById('fruit')
.innerHTML = title[i++]; // get the item and increment i to move to the next
if (i == title.length) i = 0; // reset to first element if you've reached the end
}, 1000); // 1000 milliseconds == 1 second
Run Code Online (Sandbox Code Playgroud)