1. 程式人生 > >js中的鍵盤事件:onkeydown、onkeypress、onkeyup

js中的鍵盤事件:onkeydown、onkeypress、onkeyup

相關程式碼:
<!doctype html>
<html>
    <head>
        <!--聲明當前頁面編碼集(中文編碼<gbk,gb2312>,國際編碼<utf-8>)-->
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <meta name="keywords" content="關鍵詞,關鍵詞">
        <meta name="description"
content="">
<title> html </title> <style type="text/css"> *{padding:0px;margin:0px;} </style> </head> <body> <input type="text" id="txt"/> <script> /* onkeydown----->鍵盤按下去的事件 onkeyup------->鍵盤擡起了的事件 radio checkbox focus(); */
var oTxt = document.getElementById("txt"); //oTxt.onkeyup = function(){ //alert(1); //alert(this.value); //} /* onkeydown 在 onkeypress 觸發之前 onkeypress 只會響應字母數字符號,其他的例如F1-F12、ctrl/shift/alt等不會響應 */ document.onkeypress = function
(){
console.log(2); }; document.onkeydown = function(){ console.log(1); };
</script> </body> </html>