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

jqueryweb developmentfront end technology

要检查哪个键被按下,请使用 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>

相关文章