2011-01-22 94 views
0

如何在使用jQuery按键时运行简单函数。例如,如果用户在JavaScript中按下“Q”键81,它将运行一个函数。jQuery Keycode帮助

回答

1

你想在特定文本框或页面上执行此操作?

你可以在页面做到这一点(我怀疑是你想要的)

$(function(){ 
    $(document).keypress(function(event){ 
    if(e.charCode==81){ 
     callMyFunction(); 
    } 
    }); 
}); 
2
$(document).bind('keypress', function(event) { 
    switch(e.which) { 
     case 81: { 
      alert('Q!'); 
      break; 
     } 
    } 
}); 
1

一种方法:

$(window).keydown(
    function(e){ 
     if (e.keyCode == 113 || e.keyCode == 81) { // 113 = q, 81 = Q 
      // do this 
     } 
    }); 

JS Fiddle demo

jQuery的API参考: