只有在一次显示一个数组项(JavaScript)的 [英] Only display one array item at a time (Javascript)
本文介绍了只有在一次显示一个数组项(JavaScript)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想基本上是建立一个股票,显示在同一时间从一个数组取一个字符串项。基本上我只是想让它显示一个单一的项目,然后过渡到下一个,我的JavaScript技能是基本的大量(jQuery的可能是这更好的。)
I am trying to essentially create a ticker which shows a single string item taken from an array at a time. Basically I just want it to show a single item and then transition into the next, my Javascript skills are massively rudimentary (Jquery might be better for this.)
下面是我的code:
var title= ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
for (var i=0; i<title.length; i++){
document.write(title[i]);
}
我有什么补充?
感谢一大堆!
推荐答案
通过学习的document.getElementById
和的setInterval $ C $启动C>。
HTML
<span id="fruit"></span>
使用Javascript:
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
这篇关于只有在一次显示一个数组项(JavaScript)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文