-5
我想優化我的代碼有點因爲它的問題有時不工作。 我使用3個變量來設置3個其他變量的狀態。而不是賴特代碼3次我想使用另一個變量(如(我))來設置哪一個當前活躍,但我不斷收到錯誤。我試圖尋找答案,但我可以找到任何例子。JavaScript中使用變量名稱()
代碼:
var position(1) = a;
var position(2) = a;
var position3(2) = a;
var input(1) = 250;
var input(2) = 0;
var input(3) = 0;
var i = 1;
while (z <= 3) {
if (input(i) <= minvalue + range){
position(i) = a;
}
if (input(i) <= (minvalue + (range*2)) && input(i) >= (minvalue + range)+1){
position = b;
}
if (input(i) <= (minvalue + (range*3)) && input(i) >= (minvalue + (range*2))+1){
position = c;
}
if (input(i) <= (minvalue + (range*4)) && input(i) >= (minvalue + (range*3))+1){
position(i) = d;
}
if (input(i) <= (minvalue + (range*5)) && input(i) >= (minvalue + (range*4))+1){
position(i) = e;
}
if (input(i) <= (minvalue + (range*6)) && input(i) >= (minvalue + (range*5))+1){
position(i) = f;
}
if (input(i) <= (minvalue + (range*7)) && input(i) >= (minvalue + (range*6))+1){
position(i) = g;
}
if (input(i) <= (minvalue + (range*8)) && input(i) >= (minvalue + (range*7))+1){
position(i) = h;
}
if (input(i) <= (minvalue + (range*9)) && input(i) >= (minvalue + (range*8))+1){
position(i) = i;
}
if (input(i) <= (minvalue + (range*10)) && input(i) >= (minvalue + (range*9))+1){
position(i) = j;
}
if (input(i) <= (minvalue + (range*11)) && input(i) >= (minvalue + (range*10))+1){
position(i) = k;
}
if (input(i) <= (minvalue + (range*12)) && input(i) >= (minvalue + (range*11))+1){
position(i) = l;
}
if (input(i) <= (minvalue + (range*13)) && input(i) >= (minvalue + (range*12))+1){
position(i) = m;
}
if (input(i) <= (minvalue + (range*14)) && input(i) >= (minvalue + (range*13))+1){
position(i) = n;
}
if (input(i) <= (minvalue + (range*15)) && input(i) >= (minvalue + (range*14))+1){
position(i) = o;
}
if (input(i) >= maxvalue) {
position(i) = p;
}
document.getElementById("slider" + (i)).style.top= position(i);
z = z+1;
}
}
嗯,是的,var position(1)= a'是無效的語法。你正試圖給函數調用分配一些東西,這沒有任何意義。我不確定你想用這個做什麼。 – deceze
我試圖設置輸入(1)爲一個數字,2和3相同。使用我來代替1,所以我可以在所有3個變量上運行相同的腳本,而不需要顯示3倍的代碼。基本上,代碼太長,頁面在腳本完成運行之前完成加載,所以默認位置爲a。 – AceScottie