这很难解释;如果您不能理解我的问题,请在评论中问我。
请看下面的代码。首先,我从一个数组生成随机文本,然后我有两个div
元素:current
和old
div。我想要实现的是当第一次按下开始按钮时,任何随机文本都可以显示在div 中current
。old
当第二次按下开始按钮时,old div
应该显示第一次出现的任何文本,current div
并且任何随机文本都显示在旧 div 中,然后循环继续。
我尝试old
使用 0 进行初始化,但它显示为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" />