Facing problem while printing KeyCode using jQuery
I have a small requirement that is to print the Keycode when any key is pressed.
Below is my code.. But it doesn't seems to work, Please someone help me$('document')开发者_如何学Python.keyup(function()
{
alert(event.keyCode);
});
Try this instead
$(document).keyup(function(e)
{
alert(e.keyCode);
});
Here is a demo http://jsfiddle.net/NuRWB/
If you are looking for a small utility to grab the key codes, you could do this... which has the advantage of not having to click the ok
on the alert
.
html
<input />
<div></div>
script
$('input').keyup(function(){
var kc = event.keyCode;
$('div').html(kc);
});
http://jsfiddle.net/jasongennaro/neUFS/1/
Try this:
$(document).keyup(function(event)
{
alert(event.keyCode);
});
Note that the quote marks around "document" have been removed. This is because you need to pass the JavaScript document
object to jQuery, rather than a string.
You also need to pass in event
as an argument to the event handler function.
Here is an example fiddle showing this working (make sure you click in the "Result" frame to give it focus, then press any key).
精彩评论