當用戶按鍵盤上的鍵時執行指令碼如何執行?
使用 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>
廣告