JavaScript - 跨浏览器检测 ESC 键

Uli*_*Uli 5 javascript internet-explorer keyboard-events

可能的重复:
jQuery 的转义键的键码

这段小代码对 Firefox 和 Opera 来说就像一个魅力。Internet Explorer 不喜欢它。

window.document.onkeydown = function (e) {   
    if (!e) {
        e = event;    
    }
    if (e.keyCode == 27) {
        myfunction();
    }
}
Run Code Online (Sandbox Code Playgroud)

IE 中不能检测 ESC 的按键吗?谢谢

Com*_*eek 5

您使用的是哪个版本的 IE?此代码适用于 IE (9.01):

<!doctype html>
<html>
  <head>
    <title></title>
    <script type="text/javascript">
      window.document.onkeydown = function (e)
      {
        if (!e) e = event;
        if (e.keyCode == 27)
          alert("Hello!");
      }
    </script>
  </head>
  <body>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

还要检查您的 Doctype,您的 IE 可能会使用 Quirks 模式。