0
我想寫一個腳本,在按鍵事件將從一個數組中顯示一次一個字符串。一旦數組碰到數組中的最後一項,它將循環回0位置,從而創建一個連續的循環。現在我有一個腳本會一次顯示一個項目,但它不是正確的方式,也不會循環回到開始。Jquery/Jscript:顯示從陣列中的字符串旋轉陣列
我不希望它打印每個項目作爲一個長長的清單,但在按鍵顯示第一個字符串,並在接下來的按鍵,清除DIV,並顯示在它的下一個字符串的地方
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Rotating Messages</title>
<link href="stylesheets/site.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script>
var i = 0;
var messages=["Tonight I\'m gonna have myself a real good time ",
"I feel alive and the world it\'s turning inside out Yeah! ",
"I\'m floating around in ecstasy ",
"So don\'t stop me now don't stop me ",
"Cause I\'m having a good time having a good time ",
"I\'m a shooting star leaping through the skies ",
"Like a tiger defying the laws of gravity ",
"I\'m a racing car passing by like Lady Godiva ",
"I'm gonna go go go ",
"There\'s no stopping me "]
$(document).ready(function() {
$(document).keypress(function(e) {
if (e.which===13) {
if(i<=messages.length) {
$("#lyrics").append(messages[i]);
i=i+1;
}
}
});
});
</script>
<body>
<div id="wrapper">
<header class="title">
<h1> Fun with Arrays!</h1>
<div id="lyrics"> </div>
</body>
也可以,而不是在我的函數中嵌套第三個if語句 – nope