开发者

How to prevent the default action with mootools

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE> New Document </TITLE>
    <script language="JavaScript" src="mootools-1.2.3-core-nc.js"></script>
    <script language="JavaScript" src="mootools-1.2.3.1-more-nc.js"></script>
 </HEAD>
 <BODY id='body'>
 </BODY>
  <Script> 
  function onkeydown(e){
            alert(e.key);
            e.keyCode=0;   
                    e.stop();
            return   false;   

    }
  Event.Keys.F3 = 114;
开发者_如何学编程  window.addEvent('domready', function() {
                    document.addEvent("keydown", this.onkeydown);
                                                                 }) ;
                </script>
</HTML>

when open it in IE8, pressF3, the search action comes. Why can't I stop it?


Internet Explorer, and most other browser for that matter, lock usage of the F keys from JavaScript. The rebinding of these keys is prohibited for obvious reasons; imagine the chaos if we allowed the rebinding of the F5 key!

The technical answer is that the keydown and keyup event is intercepted before it reaches the JS layer by the application. Just as you can return true or false in an event to prevent propagation, the application stops propagation of the keydown and keyup events to scripting interfaces outside of its trusted domain, eg. you can override the F3 key only inside browser Add On's and Toolbars, but not inside web pages.

In a nutshell: try rebinding another key, F keys are not easily cross-browser rebindable.


Because that is the browser's default action and there is almost never a good excuse to override expected behavior.

See How can I disable Alt-Enter in IE? for a recent discussion on disabling ALT-Enter in IE.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜