简体   繁体   English

removeEventListener() 不适用于 for 循环中的多个元素 [Javascript]

[英]removeEventListener() not working with multiple elements in a for loop [Javascript]

I've been testing around to see if you can prevent having duplicate event listeners我一直在测试,看看你是否可以防止重复的事件监听

The code below loops through all the buttons with a certain attribute.下面的代码循环遍历具有特定属性的所有按钮。

the problem is it only works with ONE button问题是它只适用于一个按钮

let LOGIC;
let removeListener = undefined;
let test = document.getElementById("test")
let allButtons = document.querySelectorAll("input[btn]")

function GameLogic(btn, test) {
test.innerHTML = eval(`${test.textContent} + 1`)
btn.value += "1"
}


let Apply = function() {
for (let i = 0; i < allButtons.length; i++) {
if (removeListener == true){
allButtons[i].removeEventListener("click", LOGIC)
}
LOGIC = GameLogic.bind(null, allButtons[i], test)
allButtons[i].addEventListener("click", LOGIC);
removeListener = true
}
}

document.getElementById("redo").addEventListener("click", Apply)

Here is the HTML:这是 HTML:

<input type = "button" id = "DIE" btn value = "Click Me">
<input type = "button" id = "DIE" btn value = "Click Me">
<input type = "button" id = "DIE" btn value = "Click Me">
<input type = "button" id = "DIE" btn value = "Click Me">
<button id = "redo"> Redo Function </button>
<p id = "test">0</p>

I've tried so many different solutions and nothing really worked.我尝试了很多不同的解决方案,但没有任何效果。

The logic seemed to add up the in the aforementioned code.逻辑似乎与上述代码相加。

The id attribute needs to be unique. id属性需要是唯一的。 You have multiple buttons with the same id ("DIE").您有多个具有相同id (“DIE”)的按钮。 That's not allowed and makes the html invalid这是不允许的,并使 html 无效

First of all, in your function Accept , the statement removeListener = true seems to be in the wrong place.首先,在您的 function Accept中,语句removeListener = true似乎在错误的位置。

let Apply = function () {
    for (let i = 0; i < allButtons.length; i++) {
        if (removeListener == true) {
            allButtons[i].removeEventListener("click", LOGIC)
        }
        LOGIC = GameLogic.bind(null, allButtons[i], test)
        allButtons[i].addEventListener("click", LOGIC);
        removeListener = true //Set as true for the first button
    }
}

Instead of:代替:

let Apply = function () {
    for (let i = 0; i < allButtons.length; i++) {
        if (removeListener == true) {
            allButtons[i].removeEventListener("click", LOGIC)
        }
        LOGIC = GameLogic.bind(null, allButtons[i], test)
        allButtons[i].addEventListener("click", LOGIC);
    }
    removeListener = true //Set as true after the first loop has run
}

Second issue is your variable LOGIC.第二个问题是您的变量逻辑。 As it is changed in every steps of the loop, when you click on Redo, you have the value for the last button instead of the first.由于它在循环的每个步骤中都会更改,因此当您单击“重做”时,您将获得最后一个按钮的值而不是第一个按钮的值。 A solution could be to use an array like this:一种解决方案可能是使用这样的数组:

let LOGICS = []; //Now is an array
let removeListener = undefined;
let test = document.getElementById("test")
let allButtons = document.querySelectorAll("input[btn]")

function GameLogic(btn, test) {
    test.innerHTML = eval(`${test.textContent} + 1`)
    btn.value += "1"
}

let Apply = function () {
    for (let i = 0; i < allButtons.length; i++) {
        if (removeListener == true) {
            allButtons[i].removeEventListener("click", LOGICS[i])//So when your remove the event listener, it is the function you have used to add it who is passed
        }
        LOGICS[i] = GameLogic.bind(null, allButtons[i], test)
        allButtons[i].addEventListener("click", LOGICS[i]);
    }
    removeListener = true
}

document.getElementById("redo").addEventListener("click", Apply)

Instead of setting removeListener as true inside the for loop, you have to put that outside.不必在 for 循环内将 removeListener 设置为 true,而必须将其放在外部。 Here you have used a LOGIC variable that is changed after every iteration and finally you've got the last value of that variable.在这里,您使用了一个 LOGIC 变量,该变量在每次迭代后都会更改,最后您获得了该变量的最后一个值。 You can use an array that works better.您可以使用效果更好的数组。

let LOGICS = [];
let removeListener = undefined;
let test = document.getElementById("test");
let allButtons = document.querySelectorAll("input[btn]");

function GameLogic(btn, test) {
    test.innerHTML = eval(`${test.textContent} + 1`);
    btn.value += "1";
}

let Apply = function () {
    for (let i = 0; i < allButtons.length; i++) {
        if (removeListener == true) {
            allButtons[i].removeEventListener("click", LOGICS[i]);
        }
        LOGICS[i] = GameLogic.bind(null, allButtons[i], test);
        allButtons[i].addEventListener("click", LOGICS[i]);
    }
    removeListener = true;
}

document.getElementById("redo").addEventListener("click", Apply);

JS array and DOM manipulation can be useful for you. JS 数组DOM 操作对你很有用。

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

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