简体   繁体   English

为什么[i]的值会立即使用for-loop跳转到数组的末尾?

[英]Why is the value of [i] jumping to the end of the array immediately using for-loop?

Trying to make a simple JS so that when the button is pushed it alerts that "You clicked it" and it changes the color of the text in the button. 尝试制作一个简单的JS,以便在按下按钮时它会提示“你点击它”,它会改变按钮中文本的颜色。 It is immediately going to the last color in the array. 它立即转到数组中的最后一种颜色。 I want it to increase incrementally so that each time I click it, it advances through the different colors. 我希望它逐渐增加,这样每次点击它,它都会前进不同的颜色。

var buttonState = document.getElementById("clickButton");

var colorArray = ["red", "blue", "green", "black", "purple", "yellow", "pink", "orange"];

//on click alert user and change color

buttonState.onclick = function() {
    alert ("You clicked it.");
    for (var i = 0; i < colorArray.length; i++) {
    colorChange = colorArray[i];
    buttonState.style.color = colorChange;
    }
}

The entire for loop is running on each click. 每次点击都会运行整个for循环。

Therefore, the button is set to the color at the end of the loop. 因此,按钮设置为循环结束时的颜色。

What you might do instead is store the current color index in a variable, and when the button is pushed, fetch the next color, like this: 你可能会做的是将当前颜色索引存储在一个变量中,当按下该按钮时,获取下一个颜色,如下所示:

var buttonState = document.getElementById("clickButton");

var colorArray = ["red", "blue", "green", "black", "purple", "yellow", "pink", "orange"];

var currentColorIndex = 0;

//on click alert user and change color

buttonState.onclick = function() {
    alert ("You clicked it.");
    currentColorIndex += 1;
    // reset to 0 if at the end of array to loop around
    if (currentColorIndex === colorArray.length) currentColorIndex=0;
    var colorChange = colorArray[currentColorIndex];
    buttonState.style.color = colorChange;
}

You are looping through all the colors very quickly always ending on the last one. 你很快就会遍历所有颜色,总是在最后一个颜色上结束。 Here's one solution to make sure that colors rotate from first to last infinitely. 这是一个确保颜色从头到尾无限旋转的解决方案。

Fiddle: http://jsfiddle.net/c4zw2doe/ . 小提琴: http//jsfiddle.net/c4zw2doe/

JS: JS:

var buttonState = document.getElementById("clickButton");

var colorArray = ["red", "blue", "green", "black", "purple", "yellow", "pink", "orange"];

var colorStart = 0;

buttonState.onclick = function() {
    alert ("You clicked it.");
    colorStart %= colorArray.length;
    buttonState.style.color = colorArray[colorStart++];
}

I think it's iterating properly over all elements, but it is executing so fast that you are not seeing all the colors. 我认为它正在迭代所有元素,但它执行得如此之快,以至于你没有看到所有的颜色。 Instead, you only see the color that was set in the end when the loop finishes. 相反,您只能看到循环结束时在最后设置的颜色。

Edit: After reading your question again, here is a short solution: 编辑:再次阅读您的问题后,这是一个简短的解决方案:

var buttonState = document.getElementById("clickButton");
var colorIndex = 0;

var colorArray = ["red", "blue", "green", "black", "purple", "yellow", "pink", "orange"];

//on click alert user and change color

buttonState.onclick = function() {
    alert ("You clicked it.");
    if(colorIndex == colorArray.length) {
        colorIndex = 0;
    }

    buttonState.style.color = colorArray[colorIndex];
    colorIndex++;
}

On user click your entire loop is executed, then the last value of colorArray is took. 在用户单击时,执行整个循环,然后执行colorArray的最后一个值。 Instead, you should increment color index on user click. 相反,您应该在用户点击时增加颜色索引。

You can do it like this: 你可以这样做:

var buttonState = document.getElementById("clickButton");

var colorArray = ["red", "blue", "green", "black", "purple", "yellow", "pink", "orange"];

var color = 0;

//on click alert user and change color

buttonState.onclick = function() {
    alert ("You clicked it.");
    buttonState.style.color = colorArray[color];
    color++;
    if(color == colorArray.length - 1) color = 0;
}

You need to keep a counter for the current position and then increment it everytime the button was clicked: 您需要为当前位置保留一个计数器,然后在每次单击按钮时递增它:

buttonState.lastIndex = -1;
buttonState.onclick = function() {
    alert ("You clicked it.");

    // this line increments the counter by 1 and performs a modulo operation to keep the index in the bounds of the colorArray.
    buttonState.lastIndex = ++buttonState.lastIndex % colorArray.length;
    colorChange = colorArray[buttonState.lastIndex];
    buttonState.style.color = colorChange;
}

On a sidenote, inside the onclick function, you can simply refer to this instead of buttonState . 在旁注中,在onclick函数内部,您可以简单地引用this而不是buttonState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM