$(window).keypress(function()) does not work in IE7?
This keypress event works fine for me in Chrome and Firefox, but simply isn't being picked up at all in IE7:
$(window).keypress(function(e) {
alert('hello world');
});
Does anyone know alternatives for 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.
IE doesn't support key events on the window
.
Place it on the document
instead.
$(document).keypress(function(e) {
alert('hello world');
});
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:
$(document).keypress(function(e) {
alert("hello world");
});
A couple things to try:
1: Wrap your event code in a document.onready event, like so:
jQuery(function()
{
$(window).keypress(function(e)....
});
This way your event is not attached until the DOM has finished loading.
2: Attach your event to the body or the document instead of the window:
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.)
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.
onkeydown
negate the keyCode and save in a persistent object
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.
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.
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.
精彩评论