[英]JavaScript: Implementing Event Remover
(New to JavaScript and the idea of closure.) (新的 JavaScript 和关闭的想法。)
I am trying to implement a Wordle clone.我正在尝试实施Wordle克隆。 The code fragment below is where I construct the on-screen keyboard buttons and add event listeners.下面的代码片段是我构建屏幕键盘按钮和添加事件侦听器的地方。 Just adding eventListener
for each button is trivial.只需为每个按钮添加eventListener
是微不足道的。 But later I have learned that, in my case, removing those event listeners are not that trivial.但后来我了解到,在我的情况下,删除那些事件监听器并不是那么微不足道。
My problem is this: if I define const handleMouseClick = processMouseClick(key);
我的问题是:如果我定义const handleMouseClick = processMouseClick(key);
inside the loop, then silenceKeyboard()
cannot access it;在循环内,则silenceKeyboard()
无法访问它; if I define outside, then I cannot add event listener.如果我在外面定义,那么我就不能添加事件监听器。
What am I supposed to do to reconcile the two?我应该怎么做才能调和这两者?
function processMouseClick(e) {
return function closure() {
if (e === "DELETE") {
backspace();
document.activeElement.blur(); //important
return;
}
else if (e === "ENTER") {
processGuess();
document.activeElement.blur();
return;
}
else {
submitEnter(e);
document.activeElement.blur();
return;
}
}
}
function silenceKeyboard() {
const keyboard = document.getElementById("user-keyboard");
const buttons = keyboard.childNodes;
buttons.forEach(button => {
button.removeEventListener("click", handleMouseClick);
});
return;
}
function createKeyboard() {
const keys = [
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "ENTER", "Z", "X", "C", "V", "B", "N", "M", "DELETE",
];
const keyboard = document.getElementById("user-keyboard");
keys.forEach((key) => {
const buttonElement = document.createElement("button");
buttonElement.textContent = key;
buttonElement.setAttribute("id", key);
keyboard.append(buttonElement);
const handleMouseClick = processMouseClick(key);
buttonElement.addEventListener("click", handleMouseClick);
});
return;
}
With minimal modification you can do something like只需进行最少的修改,您就可以做类似的事情
function silenceKeyboard() {
const keyboard = document.getElementById("user-keyboard");
const buttons = keyboard.childNodes;
buttons.forEach(button => {
button.removeEventListener("click", button._handleMouseClick);
});
return;
}
function createKeyboard() {
const keys = [
"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "ENTER", "Z", "X", "C", "V", "B", "N", "M", "DELETE",
];
const keyboard = document.getElementById("user-keyboard");
keys.forEach((key) => {
const buttonElement = document.createElement("button");
buttonElement.textContent = key;
buttonElement.setAttribute("id", key);
keyboard.append(buttonElement);
buttonElement._handleMouseClick = processMouseClick(key);
buttonElement.addEventListener(
"click",
buttonElement._handleMouseClick
);
});
return;
}
But your code can be refactored much better to have a single event listener for all keys.但是您的代码可以重构得更好,以便为所有键设置一个事件侦听器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.