简体   繁体   English

如何循环遍历按钮数组,并且只有在用户单击数组中的下一个按钮时才继续循环

[英]How do I loop through an array of buttons and only continue looping if the user clicks the next button in the array

Not sure if this is exactly the correct approach for this but I have an array of buttons that spawn on the screen and after a few seconds it changes location and I have to click the buttons in the order they appeared initially.不确定这是否是正确的方法,但我在屏幕上生成了一组按钮,几秒钟后它会改变位置,我必须按照它们最初出现的顺序单击按钮。

Following is the code :以下是代码

 let arrayButtons = []; let goButton = document.getElementById("inputButton"); function Buttons(color, height, width, top, left, order) { this.order = order; this.btn = document.createElement("button"); this.btn.style.backgroundColor = color; this.btn.style.height = height; this.btn.style.width = width; this.btn.style.position = "absolute"; document.body.appendChild(this.btn); this.setLocation = function(top, left) { this.btn.style.top = top; this.btn.style.left = left; }; this.setLocation(top, left); } function createButtons(numOfButtons) { console.log(numOfButtons); let color; let heightVal; let widthVal; let top; let left; let currentButton; for (let i = 0; i < numOfButtons; i++) { color = "#" + genRandomColor(); heightVal = "60px"; widthVal = "120px"; let x = window.innerWidth - 100; let y = window.innerHeight - 100; top = 90 + "px"; left = i * 120 + "px"; currentButton = new Buttons(color, heightVal, widthVal, top, left, i); arrayButtons.push(currentButton); } } function genRandomColor() { let randomColor = Math.floor(Math.random() * 16777215).toString(16); return randomColor; } function change() { setTimeout(function() { let x = window.innerWidth - 100; let y = window.innerHeight - 200; for (let i = 0; i < arrayButtons.length; i++) { randomX = Math.floor(Math.random() * x + 50); randomY = Math.floor(Math.random() * y + 50); arrayButtons[i].setLocation( randomX + 'px', randomY + 'px'); } }, 5000); } function isValid(range) { range = document.getElementById("textField").value; if (range < 2 || range > 10) { return false; } else { return true; } } // idea.. if the user clicks on the button in the order of the array it works // for the hint, just make the value equal to the index number + 1 function clickHandler() { let minMax = isValid(); if (minMax == true) { createButtons(document.getElementById("textField").value); change(); } else { window.alert("Must be between 2 and 10"); } } goButton.onclick = clickHandler;
 <p>How Many Buttons To Create?</p> <input id="textField" type="text"> <button type="button" id="inputButton">Go!</button> <br> <br>

HTML HTML

<button class="buttonClass">1</button>

JS JS

let btns = document.querySelectorAll(".buttonClass");
let currentBtn = 0;
btns.forEach(function(btn, crtIndex){
    btn.onclick = function(){
        if(currentBtn === crtIndex){
            currentBtn++;
            console.log("correct");
        }
    };
});

暂无
暂无

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

相关问题 当用户单击按钮时,如何 select 数组中的下一个项目? - How do I select the next item in an array when a user clicks on a button? 当用户单击按钮时,如何解构只有数据的数组? - how can i destructure an array that only has data when the user clicks the button? 当用户单击时,如何停止文本在数组中循环显示? - How do I stop my text from cycling through the array when the user clicks? 如何使用上一个/下一个按钮循环显示图像? - How do I loop through images with Previous/Next buttons? 在JavaScript中,当通过FOR循环循环时,如何将数组中该项的值传递给匿名函数? - In JavaScript, when looping through a FOR loop, how do I pass the value of the item in an array to an anonymous function? 每次用户单击按钮时,我都需要Javascript数组中的下一个值 - Every time user clicks on the button I need the next value from the array in Javascript 使用 Next 按钮生成和循环随机数数组 - Generating and Looping through random numbers array with Next button 仅在按下javascript按钮后,如何继续下一个循环迭代? - How to continue to next loop iteration only after a button press in javascript? 当用户单击其他按钮时,如何取消选择整个单选按钮组? - How do I deselect a whole group of radio buttons when a user clicks on some other button? 如何避免遍历数组以查找部分匹配项? - How do I avoid looping through an array to find a partial match?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM