【问题标题】:can not disable Ctrl+O by JavaScript in IE 11无法在 IE 11 中通过 JavaScript 禁用 Ctrl+O
【发布时间】:2014-02-28 00:48:48
【问题描述】:

我正在尝试禁用 IE 中的 Ctrl+o 组合键,以下代码在除 IE 11 之外的所有 IE 版本中都可以正常工作,除非我发出警报,如下面的代码所示:

document.onkeydown = function(event) {
    var x = event.keyCode;
    console.log(event.keyCode);
    console.log(event.ctrlKey);
    if ((x == 79) && (event.ctrlKey)) {
        if(navigator.userAgent.match(/rv:11.0/i)){
            alert('Disabled');
        }
        event.cancelBubble = true;
        event.returnValue = false;
        event.keyCode = 0;
        event.stopPropagation();
        event.preventDefault();

        return false;
    }
};

我想知道是否有其他人遇到同样的问题并且他们已经解决了。 :-) 谢谢。 亚历克斯

【问题讨论】:

  • 如果您将event.preventDefault() 移到 if 条件之外,它会阻止它吗?
  • 不会停止任何按键操作吗?我只需要禁用 Ctrl+o
  • 应该可以,这是一种检查这个问题的根源有多深的方法。 IE 可能正在做一些魔法来阻止您阻止 ctrl+o,这是找出答案的一种方法。
  • 我也有同样的问题。还没有解决方案

标签: javascript internet-explorer keyboard-shortcuts internet-explorer-11


【解决方案1】:

不幸的是,我没有好的解决方案,但我已与 Microsoft 一起创建了一个案例,并制作了一个演示该问题的 jfiddle。

我们发现的唯一方法是使用:

<meta http-equiv="X-UA-Compatible" content="IE=7">

header,但不知道什么时候会停止支持 - 更不用说在 IE7 模式下运行的明显副作用了。

一些补充说明:

小提琴:

http://jsfiddle.net/bw5sLd15/1/

// The kitchen sink
function killKey( event ) {
    event.cancelBubble = true;
    event.bubbles = false;
    event.returnValue = false;
    event.stopPropagation();
    event.stopImmediatePropagation();
    event.preventDefault();
    return false;
}

【讨论】:

    【解决方案2】:

    我得出了与 Alex & Max 相同的结论。在我的特定用例中,强制兼容模式会破坏其他功能。

    我相信在大多数情况下,确认对话框是最好的解决方法,因为它对用户来说仍然有些自然 - 除了涉及的额外步骤。

    http://jsfiddle.net/dperish/sp72c0wt/3/

    HTML:

    <h1>Demonstration of IE11 event bubbling issue</h1>
    
    <label>Enable Workaround<input type="checkbox" id="enableWorkaround"></label>
    
        <p>Pressing CTRL-P or CTRL-O should NOT show the default open/print dialogs.  The only workaround seems to be to interrupt the main thread either with alert(), confirm(), or by hitting a breakpoint in a debugger. </p>
    
     <p>Unfortunately, a synchronous/blocking XHR call was not useful for this purpose.  Nor was using the browser-specific showModalDialog.</p>
    
    
    <div id="output"></div>
    

    Javascript:

    function onKeyDown(e) {
    
        e = e || window.event;
    
        if ((e.keyCode === 79 || e.keyCode === 80) && e.ctrlKey) {
    
            e.preventDefault();
            e.stopPropagation();
            e.returnValue = false;
    
            if ($("#enableWorkaround").is(":checked")) {
                if (confirm("Run some custom method?")) {
                    customMethod(e.keyCode);
                }
            }
            else {
                customMethod(e.keyCode);
            }
            return false;
        }
    
    }
    
    function customMethod(x) {
        $("#output").append("<p>CustomMethod Says: KeyCode = " + x + "</p>");
        return false;
    }
    
    $(document).ready(function() {
    
        $(document).on("keydown", function (e) {
            onKeyDown(e);
        });
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多