[英]Only display one array item at a time (Javascript)
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.)
基本上,我只希望它显示一个项目然后过渡到下一个项目,我的Javascript技能非常初级(jQuery可能对此更好)。
Here is my code: 这是我的代码:
var title= ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
for (var i=0; i<title.length; i++){
document.write(title[i]);
}
What do I have to add? 我需要添加什么?
Thanks a bunch! 谢谢你!
Start by learning about document.getElementById
and setInterval
. 首先学习
document.getElementById
和setInterval
。
http://jsfiddle.net/wtNhf/ http://jsfiddle.net/wtNhf/
HTML: 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
http://jsfiddle.net/3fj9E/1/ http://jsfiddle.net/3fj9E/1/
js JS
var span = $('span');
var div = 0;
var fading_in = function () {
$(span[div++] || []).fadeIn(500, arguments.callee);
}
setTimeout(fading_in, 400);
html HTML
<div id='example'>
<span>orange </span>
<span>apple </span>
<span>kiwi </span>
<span>banana </span>
<span> melon</span>
</div>
css CSS
span {display:none}
I tried to keep it at its simplest 我试着把它保持在最简单的状态
HTML HTML
<div id="my_id"></div>
Javascript 使用Javascript
var counter = 0;
var words = ['jaja', 'jojo', 'lol', 'troll', 'pk!'];
var my_div = document.getElementById('my_id');
function next_word()
{
my_div.innerHTML = words[counter % words.length];
counter += 1;
}
setInterval(next_word, 100);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.