[英]Anonymous Function not working with keydown events
想知道我在做什么错-该功能应该查看表中的所有单元格,添加其默认背景色,字体颜色和边框颜色,然后向每个执行匿名函数的事件监听器添加一个事件侦听器以更改单元格的格式化取决于onmousedown上按下的键。 它正确地对所有单元进行了初始样式设置,仅此而已。
function setupPuzzle() {
allCells = document.querySelectorAll("table#hitoriGrid td");
for (var i = 0; i < allCells.length; i++) {
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
allCells[i].addEventListener("onmousedown", function(e) {
e.preventDefault();
if (e.shiftKey) {
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
} else if (e.altKey) {
allCells[i].style.backgroundColor = "rgb(0,0,0)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "0px";
} else {
allCells[i].style.backgroundColor = "rgb(101,101,101)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "50%";
}
});
}
}
简短的答案是,您正在监听onmousedown事件,因此e.altKey和e.shiftKey始终为false,因为传递给回调函数的事件是onmousedown事件。 我不确定您是否可以真正阅读是否已按下某个键,但是我想到的解决方案之一就是监听事件以进行按键按下并存储以下内容:
var keyPressed = [];
window.onkeyup = function(e) {keys[e.keyCode]=false;}
window.onkeydown = function(e) {keys[e.keyCode]=true;}
然后在发生鼠标按下事件时,您可以在阵列中检查是否按下了键。
编辑:使用其他事件所指出的事件名称使用mousedown
而不是onmousedown
。 此编辑已通过以下代码进行:
问题是i
没有指向您期望的值。 i
的范围不限于它所包含的块,因此所有onmousedown
回调都将引用一个等于allCells.length + 1
的i
在for循环的每次迭代中, i
都会增加1,并且所有onmousedown
处理程序都共享i
值,因此,在您的for循环运行结束时,处理程序将全部引用i
,即allCells.length + 1
因为这是i
在for循环退出之前所取的最后一个值。
如果使用的是ES6,则可以在for循环中使用let
而不是var
来解决此问题:
function setupPuzzle() {
allCells = document.querySelectorAll("table#hitoriGrid td");
for (let i = 0; i < allCells.length; i++) { // uset let instead of var
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
allCells[i].addEventListener("mousedown", function(e) {
e.preventDefault();
if (e.shiftKey) {
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
} else if (e.altKey) {
allCells[i].style.backgroundColor = "rgb(0,0,0)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "0px";
} else {
allCells[i].style.backgroundColor = "rgb(101,101,101)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "50%";
}
});
}
}
如果您不使用ES6,则可以使用闭包来确保i
处于块级范围(因此,所有事件处理程序都将拥有自己的i
引用):
function setupPuzzle() {
allCells = document.querySelectorAll("table#hitoriGrid td");
for (var i = 0; i < allCells.length; i++) {
(function(i) { // closure
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
allCells[i].addEventListener("mousedown", function(e) {
e.preventDefault();
if (e.shiftKey) {
allCells[i].style.backgroundColor = "rgb(255,255,255)";
allCells[i].style.color = "rgb(0,0,0)";
allCells[i].style.borderRadius = "0px";
} else if (e.altKey) {
allCells[i].style.backgroundColor = "rgb(0,0,0)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "0px";
} else {
allCells[i].style.backgroundColor = "rgb(101,101,101)";
allCells[i].style.color = "rgb(255,255,255)";
allCells[i].style.borderRadius = "50%";
}
});
})(i); // captures surrounding value of i
}
}
mousedown
代替onmousedown
i
的价值将是最后的价值i
。 所以您需要使用this
代替allCells[i]
。 function setupPuzzle() { allCells = document.querySelectorAll("table#hitoriGrid td"); for (var i = 0; i < allCells.length; i++) { allCells[i].style.backgroundColor = "rgb(255,255,255)"; allCells[i].style.color = "rgb(0,0,0)"; allCells[i].style.borderRadius = "0px"; allCells[i].addEventListener("mousedown", function(e) { e.preventDefault(); if (e.shiftKey) { this.style.backgroundColor = "rgb(255,255,255)"; this.style.color = "rgb(0,0,0)"; this.style.borderRadius = "0px"; } else if (e.altKey) { this.style.backgroundColor = "rgb(0,0,0)"; this.style.color = "rgb(255,255,255)"; this.style.borderRadius = "0px"; } else { this.style.backgroundColor = "rgb(101,101,101)"; this.style.color = "rgb(255,255,255)"; this.style.borderRadius = "50%"; } }); } } setupPuzzle();
<table id="hitoriGrid" border="2"> <tr> <td>Test</td> <td>Test</td> </tr> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.