這很難解釋;如果你不明白我的問題,請在評論中問我。在舊字符串和新字符串中存儲隨機數
請參閱下面的代碼。首先,我從一個數組中生成隨機文本,然後我有兩個div
元素:current
和old
div。我想實現的是當第一次按下開始按鈕時,任何隨機文本都可以在current
和old
div中顯示。當第二次按下開始按鈕時,無論哪個文本出現在old div
第一次應顯示在current div
和任何隨機文本顯示在舊的div和循環繼續。
我試圖用0初始化old
,但它顯示爲undefined
裏面的函數。
<script>
function randomFromTo(from, to) {
return Math.floor(Math.random() * (to - from + 1) + from);
}
var arrstring = new Array("images", "wallpaper", "photos", "vector",
"designer", "wordpress", "jquery", "extjs", "scripting", "blogging",
"search", "tagging", "digital", "javascript", "server", "hosting",
"social", "twitter", "graphic", "photoshop", "netbeans", "mysql",
"apache", "iphone", "mobile", "android", "framework", "usability",
"optimization", "interface", "developer");
function random() {
randomIndex = randomFromTo(0, arrstring.length - 1);
return randomIndex;
}
var old = 0;
function start() {
var all = old;
var old = random();
$(".current").text(arrstring[all]);
$(".old").text(arrstring[old]);
}
</script>
<div class="current">old</div>
<div class="old">new</div>
<input type="button" onclick="start();" value="start" />
謝謝:) +1對你 – Vishnu