admin管理员组文章数量:1202793
I want to get alerted whenever I press a key.
I've tried:
$('body').live('keyup', function() {
alert('testing');
});
But it doesn't work, could it be because of the selector?
UPDATE:
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".dtd">
<html xmlns="">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>TODO supply a title</title>
<script type="text/javascript" src="../system/media/js/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').live('keyup', function() {
alert('testing');
});
});
</script>
</head>
<body>
<p>
TODO write content
</p>
</body>
</html>
It doesn't alert me when I press something although it works when I replace keyup
with mouseover
and mouse over TODO write content
Why doesn't it work?
I want to get alerted whenever I press a key.
I've tried:
$('body').live('keyup', function() {
alert('testing');
});
But it doesn't work, could it be because of the selector?
UPDATE:
Here is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>TODO supply a title</title>
<script type="text/javascript" src="../system/media/js/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('body').live('keyup', function() {
alert('testing');
});
});
</script>
</head>
<body>
<p>
TODO write content
</p>
</body>
</html>
It doesn't alert me when I press something although it works when I replace keyup
with mouseover
and mouse over TODO write content
Why doesn't it work?
Share Improve this question edited Jun 26, 2017 at 14:43 Joshua 5,3222 gold badges32 silver badges46 bronze badges asked Feb 9, 2010 at 2:25 ajsieajsie 79.7k110 gold badges284 silver badges386 bronze badges 5- Are you pressing keys while the mouse is positioned inside a textbox or other input control? – Crescent Fresh Commented Feb 9, 2010 at 2:29
- no..im not having anything focused..just want to press a key and it will autofocus and paste in that keypress in an input field. if i type "hello" i want these to be shown in that input field. – ajsie Commented Feb 9, 2010 at 2:32
- Your test works for me. Can you give more info, or a demo web-page? – Sean Hogan Commented Feb 9, 2010 at 2:32
- are u typing inside any input fields? i just click outside and type something, but i get no alert...it works when i type from within a input field or use mouseover instead.. – ajsie Commented Feb 9, 2010 at 2:35
- The above code works for me. Make sure that the page has focus and also that something is not stopping event propagation (another keyup event handler). – Bryan Matthews Commented Feb 9, 2010 at 2:36
5 Answers
Reset to default 7Try using $("html")
or $("*")
instead of $("body")
. In order for the keyUp
event on body to fire, the body node or one of its children must be focused. You can accomplish this in your example by adding a text input and focusing the mouse to that input. What you really want is to capture any key press, so $("html")
should work.
Edit: I think your example might work, but in any case, to run the logic conditionally you might try this:
if ($(document.body).is(".focusOnKeypress")) {
$("html").live(...);
}
Or, I think this will also work:
$("body:not(.noFocusOnKeypress)").parent("html").live(...);
Just listen to the window!
$(window).keydown(function(event){
alert(event.keyCode);
});
I tried this code. This is working fine.
$('body').on('keyup', function() {
alert('testing');
});
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(); });
Hope this will help.
$(document.body).on('keyup', function (e) {
alert('In');
}):
本文标签: javascriptkeydown on bodyStack Overflow
版权声明:本文标题:javascript - keydown on body? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738658058a2105256.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论