如何使用 jQuery 检查按下了哪个键?


要检查按下了哪个键,请使用 _onkeydown_ 属性。

示例

你可以尝试运行以下代码来获取按下了哪个键

在线演示

<!DOCTYPE html>
<html>
<body>

<p>Press a key in the below box to get which key is pressed.</p>

<input type="text" size="10" onkeydown="demoFunction(event)">

<p id="test"></p>

<script>
  function demoFunction(event) {
    var a = event.key;
    document.getElementById("test").innerHTML = "You've pressed the key: " + a;
  }
</script>

</body>
</html>

更新于: 11-12-2019

691 次浏览

开启职业 生涯

完成课程进行认证

开始
广告
© . All rights reserved.