在 HTML 中,当用户按下按键时执行脚本?

htmlweb developmentfront end technology更新于 2025/8/14 22:04:17

使用 onkeydown  属性。onkeydown 属性会在用户按下按键时触发。

示例

您可以尝试运行以下代码,在按下按键时执行脚本 −

<!DOCTYPE html>
<html>
   <body>
      <p>Press a key inside the textbox.</p>
      <input type = "text" onkeydown = "display()">
      <script>
         function display() {
            alert("You pressed a key!");
         }
      </script>
   </body>
</html>

相关文章


有用资源