0%

问题描述

先来看一个简单问题,在控制台输入下面的代码:

1
2
3
4
// 测试平台 win10 chrome 55.0.2883.87
window.addEventListener('keydown', (e) => {
console.log(String.fromCharCode(e.which))
})

然后在页面上按小键盘上面的1键,你觉得会打印出什么?

结果为 ‘a’,why ?

问题原因

我们先看看MDN中是怎么解释 KeyboardEvent事件中的返回值

keyCode: This is usually the decimal ASCII.

which: this is usually the same as keyCode.

详细信息可以看这个 keydown

阅读全文 »