到這邊沒問題,雖然洗牌的算法...
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
把你的程式碼整理了一下
-----------------------------------------------------------------------------
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;
}