javascript $(window).keypress(function()) 在 IE7 中不起作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5024609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
$(window).keypress(function()) does not work in IE7?
提问by AP257
This keypress event works fine for me in Chrome and Firefox, but simply isn't being picked up at all in IE7:
这个按键事件在 Chrome 和 Firefox 中对我来说很好用,但在 IE7 中根本没有被接收到:
$(window).keypress(function(e) {
alert('hello world');
});
Does anyone know alternatives for IE7?
有谁知道IE7的替代品?
Or is it an error higher up in my JavaScript that means it isn't being picked up in IE7 - in which case, how can I debug it? I have script errors turned on in IE, but nothing is popping up.
或者它是我的 JavaScript 中更高的错误,这意味着它没有在 IE7 中被拾取 - 在这种情况下,我该如何调试它?我在 IE 中打开了脚本错误,但没有弹出任何内容。
回答by user113716
IE doesn't support key events on the window.
IE 不支持window.
Place it on the documentinstead.
把它放在document代替。
$(document).keypress(function(e) {
alert('hello world');
});
回答by Edgar Hernandez
Currently I have no IE7 to test it, but I believe you have to assign the event handler to $(document) instead of $(window). Something like:
目前我没有 IE7 来测试它,但我相信你必须将事件处理程序分配给 $(document) 而不是 $(window)。就像是:
$(document).keypress(function(e) {
alert("hello world");
});
回答by Luke Dennis
A couple things to try:
有几件事可以尝试:
1: Wrap your event code in a document.onready event, like so:
1:将您的事件代码包装在一个 document.onready 事件中,如下所示:
jQuery(function()
{
$(window).keypress(function(e)....
});
This way your event is not attached until the DOM has finished loading.
这样,在 DOM 完成加载之前,您的事件不会被附加。
2: Attach your event to the body or the document instead of the window:
2:将您的事件附加到正文或文档而不是窗口:
jQuery(function()
{
$("body").keypress(function(e)....
$(document).keypress(function(e)....
});
(I have no reason to think this will work, it's just something I've done when events don't bubble all the way back to document or window for some reason.)
(我没有理由认为这会起作用,这只是我在事件由于某种原因没有一直冒泡到文档或窗口时所做的事情。)
回答by Patrick Campbell
I encountered this problem too. What I discovered is that IE7 has no keyChar member in its implementation of the event object. Thus, it tries to evaluate event.keyChar, gets a null and does nothing. Instead the keyCode is ambiguous returning either the ascii keyChar or the keycode of the key that was pressed... ugh. Since I was having to trap both special keys and keys tied to characters this was my solution.
我也遇到了这个问题。我发现IE7 在其事件对象的实现中没有 keyChar 成员。因此,它尝试评估 event.keyChar,得到一个 null 并且什么都不做。相反,keyCode 是不明确的,返回 ascii keyChar 或按下的键的键码......呃。由于我不得不同时捕获特殊键和与字符相关的键,这就是我的解决方案。
onkeydown
negate the keyCode and save in a persistent object
onkeydown
否定 keyCode 并保存在持久对象中
myObject.kSave=-event.keyCode;
WHY? because at this point you don't know if the key is a character or a special key !!!
为什么?因为此时你不知道键是字符还是特殊键!!!
onkeypress
The onkeypress event may not fire if the key is a special key.
For that reason my strategy uses onkeyup to handle special keys and control characters and uses onkeypress to handle characters other than control characters.
When the key is a character, event.keyChar contains the character and its value is greater than 0. However, since IE7 doesn't have an event.keyChar member, use event.keyCode as the character. You can force all control characters (e.g. enter, esc, tab, etc.) to be handled as a special key by preventing the default behavior of control characters and deferring handling to onkeyup. To signal that a character has been handled we must set the saved object to the character we found.
onkeypress
如果键是特殊键,则 onkeypress 事件可能不会触发。出于这个原因,我的策略使用 onkeyup 处理特殊键和控制字符,并使用 onkeypress 处理控制字符以外的字符。当key为字符时,event.keyChar包含该字符且其值大于0。但是,由于IE7没有event.keyChar成员,所以使用event.keyCode作为字符。您可以通过阻止控制字符的默认行为并将处理推迟到 onkeyup 来强制将所有控制字符(例如 enter、esc、tab 等)作为特殊键处理。为了表示字符已被处理,我们必须将保存的对象设置为我们找到的字符。
var ch = (event.keyChar == null) ? event.keyCode : event.keyChar;
if (ch >=32) {//ch is a character and not a control character
myObject.kSave = ch; //tells onkeyup that the key was handled
event.preventDefault(); //don't let default processing mess with your handler
switch(ch) {
//your character handler goes here
}
} else if (ch > 0) {//ch is a control character
event.preventDefault();
}
onkeyup
Add the saved value to event.keyCode. If the sum is zero, the onkeypress event did not
handle the keypress because the pressed key was a special key or a control character.
When the sum is positive, there is nothing further to do because the key was a character
and the onkeypress event handler already took care of it.
onkeyup
将保存的值添加到 event.keyCode。如果总和为零,则 onkeypress 事件不会处理按键,因为按下的键是特殊键或控制字符。当总和为正时,没有什么可做的,因为键是一个字符并且 onkeypress 事件处理程序已经处理了它。
if (myObject.kSave+event.keyCode == 0) {//this is a special key or control character
event.preventDefault();
switch(event.keyCode) {
//handle special keys here
}
}
One last thing, in your event handler invoke event.preventDefault() if you don't want the default handler to mess with your event handler.
最后一件事,如果您不希望默认处理程序与您的事件处理程序混淆,请在您的事件处理程序中调用 event.preventDefault()。

