2

我试图从本质上创建一个代码,它一次显示一个从数组中获取的单个字符串项。基本上我只希望它显示一个项目,然后过渡到下一个项目,我的 Javascript 技能非常初级(Jquery 可能会更好。)

这是我的代码:

var title= ['Orange', 'Apple', 'Mango', 'Airplane', 'Kiwi'];
for (var i=0; i<title.length; i++){
document.write(title[i]);
}

我要补充什么?

非常感谢!

4

3 回答 3

9

从了解document.getElementById和开始setInterval

http://jsfiddle.net/wtNhf/

HTML:

<span id="fruit"></span>

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
于 2013-03-28T21:03:52.190 回答
0

http://jsfiddle.net/3fj9E/1/

js

 var span = $('span');
 var div = 0;
 var fading_in = function () {
 $(span[div++] || []).fadeIn(500, arguments.callee);
 }
setTimeout(fading_in, 400);

html

 <div id='example'>
 <span>orange </span>
 <span>apple </span>
 <span>kiwi </span>
<span>banana </span>
<span> melon</span>
</div>

css

 span {display:none}
于 2013-03-28T21:04:03.973 回答
0

我试图让它保持最简单

HTML

<div id="my_id"></div>

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);

http://jsfiddle.net/zTLqe/

于 2013-03-28T21:10:40.253 回答