Javascript循环文本

Javascript cycling text

本文关键字:文本 循环 Javascript      更新时间:2023-09-26

我用过它,它运行得很完美。。。现在我不明白为什么它停止工作。。。我不是程序员,所以如果我错误地删除了一些东西,我很难找到它……我确实查看了所有内容。。。我觉得一切都很好……你能告诉我为什么我现在有未定义的错误吗?

我的标题是:

<script type="text/javascript" src="js/textCycle.js"></script>

这个在我的身体标签:

<body onload="changeText()">

这就是脚本应该在的位置:

<table border = 0><tr><td style="width:300px;" height="100px"> <!-- Change the height in order to determine width of quotes -->
    <div id="change"></div></td></tr></table>

这是我的JS:

var quotes=new Array(5);
var i = 0;
var authors=new Array(5);
//Load Quotes into array
quotes[0]="'"Temoignage #1'"";authors[0]="Client #1";
quotes[1]="'"Temoignage #2'"";authors[1]="Client #2";
quotes[2]="'"Temoignage #3'"";authors[2]="Client #3";
quotes[3]="'"Temoignage #4'"";authors[3]="Client #4";
quotes[4]="'"Temoignage #5'"";authors[4]="Client #5";

//Call the changeText() function every 5000 miliseconds
setInterval(changeText, 3000);
//Function that determine what quote and author to put in html.
function changeText(){
    document.getElementById("change").innerHTML=(quotes[i] + '<p style="text-align: right"><i>' + authors[i] + '</i></p>');
    if(i == 4)
        i = 0;
    else
        i++;
}

我在JS中更改了一些内容,它正在工作。。。做了一些事情后,它突然停止了工作(这是我记不起来的事情!)我想知道我的代码出了什么问题?

谢谢你的帮助!

页面上似乎有另一个全局i变量,该变量被设置为比quotes数组大小大的任意高的数字,因此它是未定义的。

i重命名为更具描述性的内容,如currentClientIndex,您应该会做得很好。

相关文章: