【问题标题】:prevent default 'F1' event in iE11防止 iE11 中的默认“F1”事件
【发布时间】:2015-05-30 11:32:54
【问题描述】:

当用户按下 F1 键时,我打算显示我们的应用程序帮助并禁止默认操作。 我尝试使用不同的选项不显示 IE 的帮助弹出窗口。 这是我的代码:

document.addEventListener('keydown', function (e) {
            if (e.key === 'F1' || e.keyCode == 112) {

                   e.cancelBubble = true;
                    e.cancelable = true;
                    e.stopPropagation();
                    e.preventDefault();
                    e.returnValue = false;

                //my help menu code goes here
            }
});

请告诉我如何才能显示我的应用程序的帮助页面而不是 IE 帮助。 我用的是IE11版本。

【问题讨论】:

  • 你试过event.originalEvent.keyCode = 0;,根据this answer它在IE8中工作
  • 我看不到 event.originalEvent
  • IE11: event.originalEvent => 未定义

标签: javascript jquery html internet-explorer dom


【解决方案1】:

您可以订阅window.onhelp 事件:

window.onhelp =function() { 
    alert();
    return false;
}

【讨论】:

  • 感谢您的支持。我无法在窗口上看到 onhelp()。
  • 我用上面的代码试过了,但无法实现
  • 请发表你的整个js
  • 这是我的 js 代码:function showNMCHelp() { var dialog = NMCApp.addDialog(helpTitle, 800, 400); dialog.load('/' + NMCApp.getVirtualDirectoryName() + helpLink, null, true); } document.addEventListener('keydown', function (e) { if (e.key === 'F1' || e.keyCode == 112) { e.cancelable = true; e.stopPropagation(); e.preventDefault (); e.returnValue = false; showNMCHelp(); } });
  • 我已经使用了你的代码,我在发布之前删除了它,但它不起作用
【解决方案2】:

试试这个

<script>
        $(document).ready(function () {

            removedefaulthelp();
            function removedefaulthelp()
            {
                window.onhelp = function () {
                    return false;
                    alert();
                }
            }
            document.addEventListener('keydown', function (e) {
                if (e.key === 'F1' || e.keyCode == 112) {
                    removedefaulthelp();
                    e.cancelBubble = true;
                    e.cancelable = true;
                    e.stopPropagation();
                    e.preventDefault();
                    e.returnValue = false;
                    //my help menu code goes here
                }
            });
}
</script>

更多信息请参考this

【讨论】:

  • 感谢您的帮助。我尝试了您的代码似乎在 IE11 中没有任何工作正常。
【解决方案3】:

这是一个类似于Sukanya'sanswer 的示例,但我的解决方案显示了如何扩展 F2-F12 键,并故意忽略 F 组合键,例如 CTRL + F1 .

<html>
<head>
<!-- Note:  reference your own JQuery library here -->
<script type="text/javascript" src="jquery-1.6.2.min.js"></script>
</head>
<body>
    <h1>F-key trap example</h1>
    <div><h2>Example:  Press the 'F1' key to open help</h2></div>
    <script type="text/javascript">
        //uncomment to prevent on startup
        //removeDefaultFunction();          
        /** Prevents the default function such as the help pop-up **/
        function removeDefaultFunction()
        {
            window.onhelp = function () { return false; }
        }
        /** use keydown event and trap only the F-key, 
            but not combinations with SHIFT/CTRL/ALT **/
        $(window).bind('keydown', function(e) {
            //This is the F1 key code, but NOT with SHIFT/CTRL/ALT
            var keyCode = e.keyCode || e.which;
            if((keyCode == 112 || e.key == 'F1') && 
                    !(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey))
             {
                // prevent code starts here:
                removeDefaultFunction();
                e.cancelable = true;
                e.stopPropagation();
                e.preventDefault();
                e.returnValue = false;
                // Open help window here instead of alert
                alert('F1 Help key opened, ' + keyCode);
                }
            // Add other F-keys here:
            else if((keyCode == 113 || e.key == 'F2') && 
                    !(event.altKey ||event.ctrlKey || event.shiftKey || event.metaKey))
             {
                // prevent code starts here:
                removeDefaultFunction();
                e.cancelable = true;
                e.stopPropagation();
                e.preventDefault();
                e.returnValue = false;
                // Do something else for F2
                alert('F2 key opened, ' + keyCode);
                }
        });
    </script>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多