2011-03-22 62 views

回答

5

您必須在document對象上捕獲按鍵事件。

$(document).keydown(function(e){ 
    if (e.keyCode == 37) { 
     // do something when left arrow is pressed 
     return false; 
    } 
}); 

字符代碼:

37 - 左

39 - 右

40 - 向下

1
$(document).keydown(function (evt) { alert(evt.which); }); 

現在你可以找到'左'和'右'的代碼(只需按下它們)並使用它們;)。

+0

他們是37(左)和39(右)(我只是測試它)。現在你可以這樣做: $(document).keydown(function(evt){if(evt.which == 37){alert('Left pressed!');}}); – 2011-03-22 11:11:08

+0

真的嗎?哦......太容易了! – Steffi 2011-03-22 12:38:17