简体   繁体   English

防止IE9中的默认密钥处理

[英]Prevent default key handling in IE9

I want to disable some default browser keys (CTRL + F, ALT + ENTER) as they do not make sense in my application scenario. 我想禁用一些默认的浏览器键(CTRL + F,ALT + ENTER),因为它们在我的应用程序场景中没有意义。 What I've read so far the most important thing to disable these events in IE9 is to set the keyCode to 0 in this case, however I always get an "Access denied" error when I do this. 到目前为止我所读到的最重要的事情是在IE9中禁用这些事件是在这种情况下将keyCode设置为0,但是当我这样做时,我总是得到一个“拒绝访问”错误。

This is my code: 这是我的代码:

var fnOnKeyDown = function(event)
{
  if(!event)
  {
    event = window.event;
  }
  if(event.preventDefault)
  {
    event.preventDefault();
  }
  event.returnValue = false;
  event.cancelBubble = true;
  event.keyCode = 0;      
  return false;  
};

window.document.onkeydown = fnOnKeyDown;

As mentioned, the line event.keyCode = 0 throws an error "Access denied". 如上所述,行event.keyCode = 0抛出错误“拒绝访问”。 When I remove it or put it into an empty try/catch block it doesn't throw the error but the default browser key handling is not suppressed anymore. 当我删除它或将其放入一个空的try / catch块时,它不会抛出错误,但不再抑制默认的浏览器键处理。

are you sure you don't mean ie7? 你确定你不是说ie7吗? I didn't seem to have a problem in ie9... 我在ie9中似乎没有问题......

anyway, here is a post from microsoft on your issue 无论如何,这是一篇关于你的问题的微软帖子

http://support.microsoft.com/kb/934364 http://support.microsoft.com/kb/934364

To resolve this problem, change the code that assigns the event.keyCode property so that it does not change the value. 若要解决此问题,请更改分配event.keyCode属性的代码,以便它不会更改该值。 The change in the code lets you use the SHIFT key or the CTRL key to load pages from the local disk 代码中的更改允许您使用SHIFT键或CTRL键从本地磁盘加载页面

Alternatively, you can host the Web pages on a Web server. 或者,您可以在Web服务器上托管Web页面。 This makes sure that the event.keyCode property in the script runs correctly. 这可确保脚本中的event.keyCode属性正确运行。

also, if possible I would advise the use of jquery. 另外,如果可能的话,我会建议使用jquery。 frameworks like that often handle various browser issues for you through a common interface. 像这样的框架通常通过一个通用接口为您处理各种浏览器问题。

event.preventDefault()event.stopPropagation()应该足够了 - 您不需要更改事件本身的任何属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM