简体   繁体   English

如何使用 mootools 阻止默认操作

[英]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, press F3 , the search action comes.在 IE8 中打开它,按F3 ,搜索动作来了。 Why can't I stop it?为什么我阻止不了?

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?请参阅如何在 IE 中禁用 Alt-Enter? for a recent discussion on disabling ALT-Enter in IE.最近关于在 IE 中禁用 ALT-Enter 的讨论。

Internet Explorer, and most other browser for that matter, lock usage of the F keys from JavaScript. Internet Explorer 和大多数其他浏览器都锁定了 JavaScript 中 F 键的使用。 The rebinding of these keys is prohibited for obvious reasons;出于显而易见的原因,禁止重新绑定这些密钥; imagine the chaos if we allowed the rebinding of the F5 key!想象一下如果我们允许重新绑定 F5 键的混乱!

The technical answer is that the keydown and keyup event is intercepted before it reaches the JS layer by the application.技术上的答案是keydown和keyup事件在到达JS层之前被应用程序拦截。 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.正如您可以在事件中返回 true 或 false 以防止传播一样,应用程序会停止将 keydown 和 keyup 事件传播到其受信任域之外的脚本接口,例如。 you can override the F3 key only inside browser Add On's and Toolbars, but not inside web pages.您只能在浏览器附加组件和工具栏内覆盖 F3 键,但不能在网页内覆盖。

In a nutshell: try rebinding another key, F keys are not easily cross-browser rebindable.简而言之:尝试重新绑定另一个键,F 键不容易跨浏览器重新绑定。

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

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