JavaScript - 跨浏览器检测 ESC 键
可能的重复:
jQuery 转义键的键码
这段代码的工作原理如下Firefox 和 Opera 的魅力所在。 Internet Explorer 不喜欢它。
window.document.onkeydown = function (e) {
if (!e) {
e = event;
}
if (e.keyCode == 27) {
myfunction();
}
}
IE 中不能检测 ESC 的按键吗? 谢谢
Possible Duplicate:
Which keycode for escape key with jQuery
This little piece of code works like a charm for Firefox and Opera.
Internet Explorer doesn't like it.
window.document.onkeydown = function (e) {
if (!e) {
e = event;
}
if (e.keyCode == 27) {
myfunction();
}
}
Isn't it possible to detect keydown for ESC in IE?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是哪个版本的 IE?
此代码适用于 IE (9.01):
另请检查您的 Doctype,您的 IE 可能会使用 Quirks 模式。
Which version of IE do you have?
This code works for me with IE (9.01):
Check also your Doctype, your IE could use the Quirks mode, maybe.
一些浏览器使用 e.keyCode,其他浏览器使用 e.charCode。此外,这因事件类型而异,某些浏览器(例如 FF)将使用一个用于一种类型的键,而另一种用于另一种类型(例如字母与箭头键)
但是,在 jsfiddle 中测试这一点,看起来像e.keyCode 是 IE9 的正确检查。您使用哪个版本的 IE 进行测试?
Some browsers use e.keyCode, others e.charCode. Also, this varies for the type of event, some browsers (like FF) will use one for one type of key, and the other for another type (such as letters vs. arrow keys)
However, testing this in jsfiddle, it looks like e.keyCode is the right check for IE9. Which version of IE are you testing with?