JavaScript AddeventListener无参数工作
我一直在寻找JavaScript文档,发现这是
const log = document.getElementById('log');
document.addEventListener('keydown', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}
我不明白logkey函数在AddeventListener中的工作原理。当我按键时,控制台会打印其代码,但是LogKey在AddeventListener中没有任何参数。它是如何打印E.Code的?
I was looking for javascript documentation and found this
const log = document.getElementById('log');
document.addEventListener('keydown', logKey);
function logKey(e) {
log.textContent += ` ${e.code}`;
}
I don't understand how logkey function is working in addeventlistener . when I press a key, the console prints its code but logkey is not having any parameters in addeventlistener. How did it print e.code ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将功能作为参数传递给EventListener。 EventListener将logkey函数与事件一起以功能参数为单位。
在以下示例中,请参见如何将参数传递到该函数:
You pass the function as a parameter to the eventListener. The eventListener calls the logKey function with the event as a function parameter.
See in the following example how a parameter is passed on to the function: