【发布时间】:2020-12-12 04:32:29
【问题描述】:
我有一个有 14 个按钮的应用程序。当您单击任何按钮时,这会创建另一个具有相同内部文本的按钮。
所有预先存在的按钮都有一个数字类,可以帮助我遍历它们。我正在尝试这样做,以便当单击其中两个或更多按钮时,它会按从左到右的升序组织新的“副本”按钮。
就目前而言,我的代码对前两个“副本”按钮进行了正确排序,但是当您总共单击三个或更多预先存在的按钮时,它会忽略该顺序。
我怀疑发生这种情况是因为一旦按钮最初呈现,循环就会中断,但我无法解决这个问题。你能帮忙吗?
这是我认为导致问题的代码部分:
function orderButtons(array) {
for (i in array) {
let sideElements = array[i];
if (array.length >= 2) {
sortNumbers(array);
/*let classArray = this.className.split(' ')
let buttonNum = +(classArray[0]);
let index = sideArray.indexOf(buttonNum);*/
choiceButtons(sideButtons[sideElements].textContent);
console.log(array);
}
}
}
编辑:这是可能导致问题的另一部分代码:
function choiceButtons(innerText) {
//create button
if (intervalArray.indexOf(innerText) === -1) {
let choiceBtn = document.createElement("BUTTON");
//chooseDiv.style.visibility ="hidden";
//document.getElementById('start-button').disabled = true; //add inner text to choice buttons
choiceBtn.textContent = innerText;
//append to choice-buttons-div
document.getElementById("choice-buttons-div").appendChild(choiceBtn);
choiceBtn.className = "choice-buttons";
//push innerText to array
intervalArray.push(innerText);
console.log(intervalArray);
let childNodes = chooseDiv.childNodes;
console.log(intervalArray);
}
}
这是笔:https://codepen.io/david-webb/pen/abNmbVm?editors=1010
我添加了一个清除功能来暂时删除按钮。然后我调用 orderButtons 函数来尝试重新渲染按钮,但这没有奏效。
【问题讨论】:
标签: javascript arrays sorting dom createelement