keydown on body?

前端 未结 5 1188
礼貌的吻别
礼貌的吻别 2020-12-20 22:53

I want to get alerted whenever I press a key.

I\'ve tried:

$(\'body\').live(\'keyup\', function() {
     alert(\'testing\');
});

Bu

5条回答
  •  一向
    一向 (楼主)
    2020-12-20 23:06

    It does not work because your page does not have focus.. you need to click on the page first and it will work..

    alternatively you could forcibly set the focus to an input element and thus bring focus to the page..

    $(function(){ $('input_selector_here').focus(); });
    

提交回复
热议问题