3

我想禁用一些默认浏览器键(CTRL + F,ALT + ENTER),因为它们在我的应用程序场景中没有意义。到目前为止,我所读到的在 IE9 中禁用这些事件的最重要的事情是在这种情况下将 keyCode 设置为 0,但是当我这样做时,我总是会收到“拒绝访问”错误。

这是我的代码:

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;

如前所述,行 event.keyCode = 0 会引发错误“访问被拒绝”。当我将其删除或将其放入空的 try/catch 块时,它不会引发错误,但不再抑制默认的浏览器键处理。

4

2 回答 2

1

are you sure you don't mean ie7? I didn't seem to have a problem in ie9...

anyway, here is a post from microsoft on your issue

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. The change in the code lets you use the SHIFT key or the CTRL key to load pages from the local disk

Alternatively, you can host the Web pages on a Web server. This makes sure that the event.keyCode property in the script runs correctly.

also, if possible I would advise the use of jquery. frameworks like that often handle various browser issues for you through a common interface.

于 2013-04-21T19:19:08.360 回答
1

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

于 2013-04-27T11:16:13.160 回答