我目前有一個JavaScript文件,它將每五秒鐘更改一次證明。一切都很完美,除了前五秒外,沒有任何東西出現。如果我在JavaScript函數被調用的地方放置了一個值,它最初會顯示出來,然後被第一個證明文件替換。JavaScript在X秒內更改文本 - 無初始顯示
以下是調用JavaScript的HTML代碼。
<html>
<head>
<SCRIPT language="JavaScript" SRC="textCycle.js"></SCRIPT>
</head>
<body>
<table border = 0><tr><td style="width:300px;"> <!-- Change the height in order to determine width of quotes -->
<div id="change"></div></td></tr></table>
</body>
</html>
這裏是JavaScript:
var quotes=new Array(5);
var i = 0;
var authors=new Array(5);
//Load Quotes into array
quotes[0]="\"Website is awesome!\"";
quotes[1]="\"Love it!\"";
quotes[2]="\"Awesome site!\"";
quotes[3]="\"This site was very informative and helped with my problem.\"";
quotes[4]="\"Best site for helping with this issue.\"";
//Load authors that correspond with the quote array
authors[0]="Anonymous";
authors[1]="Anonymous";
authors[2]="Anonymous";
authors[3]="Anonymous";
authors[4]="Anonymous";
//Call the changeText() function every 5000 miliseconds
setInterval(changeText, 5000);
//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++;
}
這只是改變了JavaScript文件的問題,這樣的報價[0]是循環之外?
注意:數組中的值已更改爲匿名。這些不是真正的推薦。
與你的問題沒有關係,但我建議使用一個對象數組來保持作者鏈接到報價。 'var quote ='{quote:''Love it!'',author:'Anonymous'}/*,etc .. * /];'這將使管理和更新報價變得更容易。 – 2013-02-20 17:36:16