Change background color with a loop onclick
问题 here is my js fiddle : http://jsfiddle.net/pYM38/16/ var box = document.getElementById('box'); var colors = ['purple', 'yellow', 'orange', 'brown', 'black']; box.onclick = function () { for (i = 0; i < colors.length; i++) { box.style.backgroundColor = colors[i]; } }; I'm in the process of learning JavaScript. I was trying to get this to loop through each color in the array, but when i click the box (demonstration on jsfiddle) it goes to the last element in the array. 回答1: Here are two methods