一位大一新生在一项任务中陷入困境.我肯定做错了,因为我在keyPressed上出现了错误,因为事件没有定义,下面的代码如下所示。如何在此实例中定义事件?
function keyPressed()
{ console.log("keyPressed");
// When any key is pressed:
// - Make RestrictedSafeComb2 equal to the value of 'keyCode'
event.keyCode = 'enter'
RestrictedSafeComb2 = event.keyCode;
}发布于 2021-06-12 20:12:35
首先,不推荐使用keypress事件。我不知道你的作业是什么性质的,但你可以用keydown代替。
另外,keydown是您希望代码监视和使用其属性的事件。我想KeyboardEvent.code是你想要的。
关于MDN文档的更多详细信息:https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
document.addEventListener('keypress', keyPressed);
function keyPressed(e) {
console.log (e.code)
}<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
</body>
</html>
https://stackoverflow.com/questions/67947747
复制相似问题