作者eight0 (欸(/‵Д′)/~ ╧╧)
看板Ajax
标题Re: [问题] 网页没有更新
时间Tue May 22 01:15:06 2012
把你的程式码整理了一下
-----------------------------------------------------------------------------
function computeRandom(){
var value = new Array(49);//declare array
for ( i = 0; i < 49; i++ )//initial array_value
value[i] = i+1;
for ( i = 0 ;i < 100; i++ ) {//random arrange
x = parseInt(Math.random()*49);
y = parseInt(Math.random()*49);
tmp = value[x];
value[x] = value[y];
value[y] = tmp;
}
到这边没问题,虽然洗牌的算法...
var color = new Array(49);
for ( i = 0; i < 49; i++ )//store color
color[i] = "rgb(" + parseInt(Math.random()*255) + "," +
parseInt(Math.random()*255) + ","
+ parseInt(Math.random()*255) + ")";
var value_tmp = new Array(6);
for( i = 0; i < 6; i++)
value_tmp[i] = value[i];
document.write("<div style = \"text-align:center;\" >");
以後会知道要把 HTML 和 JavaScript 分开。不过问题不在这。
//center the text by div
for( i = 0; i < 6; i++)
document.write("<span style = \"font-size: 2.5em; dis\
play:inline-block;
text-align:center;
width: 1.5em;
background: white;
color: " + color[i] + " \" > " + value_tmp[i]
+ "</span>     ");
多行字串的表达法有两种,你可以在字串尾端加一个「\」跳脱掉换行符(不建议),或
是用「+」串联多个字串。
var spanArray = document.getElementsByTagName("span");
setInterval("keepMove(value,spanArray)",10);
最大的问题在这... setInterval 如果传字串进去,会在全域执行,也就是
window.keepMove(window.value,window.spanArray);
但你的 value 与 spanArray 都是区域变数,所以 keepMove 收到的参数都是
undefined 。
另外就是你的 div 标签没有关闭。
}
function keepMove(val,sp){
var index = parseInt(Math.random()*43);
//set a increment to avoid repeatition
for( i = 0; i < sp.length; i++){
sp[i].innerHTML = val[i+index];
document.write(sp[i].innerHTML+" ");
这行就不知道是什麽了...
}
}
-----------------------------------------------------------------------------
把上述的地方改掉後︰
http://jsbin.com/utulif/edit#preview
--
(* ̄▽ ̄)/‧★*"`'*-.,_,.-*'`"*-.,_☆,.-*`
http://i.imgur.com/oAd97.png
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 114.24.76.158