Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法禁用Ctrl+;O在ie11中使用JavaScript_Javascript_Internet Explorer_Keyboard Shortcuts_Internet Explorer 11 - Fatal编程技术网

无法禁用Ctrl+;O在ie11中使用JavaScript

无法禁用Ctrl+;O在ie11中使用JavaScript,javascript,internet-explorer,keyboard-shortcuts,internet-explorer-11,Javascript,Internet Explorer,Keyboard Shortcuts,Internet Explorer 11,我正在尝试禁用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)) {

我正在尝试禁用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;
    }
};
我想知道是否有其他人也遇到了同样的问题,他们已经解决了。:-) 谢谢
Alex

不幸的是,我没有好的解决方案,但我已经与微软建立了一个案例,并制作了一个jfiddle来演示这个问题

我们找到的唯一解决方法是使用:

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

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

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

HTML:

演示IE11事件冒泡问题
启用变通方法
按CTRL-p或CTRL-O不应显示默认的打开/打印对话框。唯一的解决方法似乎是使用alert()、confirm()或在调试器中点击断点来中断主线程

不幸的是,同步/阻塞XHR调用对此没有用处。也没有使用特定于浏览器的showModalDialog

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);
    });

});
函数onKeyDown(e){
e=e | | window.event;
if((e.keyCode==79 | | e.keyCode==80)和&e.ctrlKey){
e、 预防默认值();
e、 停止传播();
e、 returnValue=false;
如果($(“#启用解决方案”)。为(“:选中”)){
if(确认(“运行一些自定义方法?”){
自定义方法(如键码);
}
}
否则{
自定义方法(如键码);
}
返回false;
}
}
函数自定义方法(x){
$(“#输出”).append(CustomMethod表示:KeyCode=“+x+”

”); 返回false; } $(文档).ready(函数(){ $(文档).on(“向下键”,函数(e){ onKeyDown(e); }); });
如果将
事件.preventDefault()移动到If条件之外,它会阻止它吗?这会停止任何按键吗?我只需要禁用Ctrl+oIt,这是一种检查问题根源有多深的方法。IE可能正在做一些魔法来阻止你阻止ctrl+o,这是一种找到答案的方法。我也有同样的问题。还没有解决办法
<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>
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);
    });

});