我想通過我到inputItems [i] .on所以我可以將它設置爲執行特定的任務(由createSelectedInputItem(i)定義),你如何將變量傳入函數(){..operation here}?範圍與JavaScript函數
for(var i=0; i< 6; i++){
console.log("setting mouse event for : " + i);
// Bring in all the input items
inputItems[i].on('click', function() {
console.log("i is still:" + i);
input.tween.reverse();
console.log("pressed" + i);
createSelectedInputItem(i);
for(var j=0; j< 6; j++){
inputItems[j].tween.reverse();
}
});
}
LOG
//當頁面被加載
setting mouse event for : 0
setting mouse event for : 1
setting mouse event for : 2
setting mouse event for : 3
setting mouse event for : 4
setting mouse event for : 5
//當按下inputItems之一
i是仍然:6
pressed6
這甚至不假設前北京時間
可能的重複:http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example –