使用onkeydown 属性。当用户按下按键时,onkeydown属性将触发。
您可以尝试运行以下代码来在按下键时执行脚本-
<!DOCTYPE html>
<html>
<body>
<p>Press a key inside the textbox.</p>
<input type = "text" onkeydown = "display()">
<script>
function display() {
alert("您按下了一个键!");
}
</script>
</body>
</html>