防止默认事件操作不起作用...?

发布于 2024-12-11 20:43:05 字数 904 浏览 0 评论 0 原文

我正在尝试在我的网站上添加键盘快捷键,以便使用键盘进行快速导航。然而,我尝试使用 Alt+X 快捷键时遇到了一个小问题。该事件运行良好并返回 false ,但浏览器的文件菜单无论如何都会出现。我也尝试过 preventDefault 方法,但没有任何变化。

该脚本的简化版本是:

document.documentElement.onkeydown = function(e) {
    e = e || window.event;
    switch( e.keyCode || e.which) {
        // some cases here - most notably:
        case 116: // F5 key
            if( activeFrame) {
                activeFrame.contentWindow.location.reload();
                // reloads an iframe if one is active
                return false;
            }
            break;
        // more cases...
        case 88: // X key
            if( e.altKey) {
                // do something
                return false;
            }
    }
}

如上所述,覆盖 F5 键的默认操作效果很好 - 仅当没有 iframe 处于活动状态时,浏览器才会重新加载页面。我不太明白如何防止按下 Alt+X 时出现菜单。

I'm trying to add keyboard shortcuts on my website to make fast navigation possible using the keyboard. I'm running into a slight problem, however, with my attempted Alt+X shortcut. The event runs just fine and returns false as it should, but the browser's File menu comes up regardless. I've also tried the preventDefault method, but no change.

The cut-down version of the script is:

document.documentElement.onkeydown = function(e) {
    e = e || window.event;
    switch( e.keyCode || e.which) {
        // some cases here - most notably:
        case 116: // F5 key
            if( activeFrame) {
                activeFrame.contentWindow.location.reload();
                // reloads an iframe if one is active
                return false;
            }
            break;
        // more cases...
        case 88: // X key
            if( e.altKey) {
                // do something
                return false;
            }
    }
}

As noted above, overriding the default action of the F5 key works just fine - the browser reloads the page only if no iframe is active. I don't quite see how to prevent the menu from appearing when Alt+X is pressed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何其悲哀 2024-12-18 20:43:05

使用 stopPropagation(e); 代替 preventDefault 方法

function stopPropagation(e)
{
    e = e || event;/* get IE event ( not passed ) */
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true;
}

参考链接

另一个SO问题提到preventDefault 在 IE 中存在问题。

更新

尝试按照 MSDN 参考使用下面的代码< /a>

event.returnValue=false;

以及检测击键中的一些观点

一些一般注意事项:

  • 通常,Mac 的可靠性不如 Windows,并且无法检测到某些按键。
  • 资源管理器不会触发删除、结束、输入、转义、功能键、主页、插入、向上/向下翻页和选项卡的按键事件。
  • Onkeypress,Safari 为删除、结束、功能键、home 和 pageUp.Down 提供了 63200 范围内的奇怪 keyCode 值。 onkeydown 和 -up 值正常。
  • 除 Opera 外,Mac 上无法检测到 Alt、Cmd、Ctrl 和 Shift。但是,您始终可以使用 altKey、ctrlKey 和 shiftKey 属性。

use stopPropagation(e); instead of preventDefault method

function stopPropagation(e)
{
    e = e || event;/* get IE event ( not passed ) */
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true;
}

Reference link

Another SO question which mentions that preventDefault has issue in IE.

UPDATE

Try using below code as per MSDN Reference

event.returnValue=false;

And some point from Detecting keystrokes

Some general caveats:

  • Generally, Mac is less reliable than Windows, and some keys cannot be detected.
  • Explorer doesn't fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.
  • Onkeypress, Safari gives weird keyCode values in the 63200 range for delete, end, function keys, home and pageUp.Down. The onkeydown and -up values are normal.
  • Alt, Cmd, Ctrl and Shift cannot be detected on Mac, except in Opera. However, you can always use the altKey, ctrlKey, and shiftKey properties.
烏雲後面有陽光 2024-12-18 20:43:05

实际上,我有一个 Web 应用程序可以使用 CTRL 快捷键正常工作,但后来我决定聪明地使用 accesskey 属性,并在 IE 中遇到了同样的问题。

使用 CTRL 快捷键的问题在于,其中许多快捷键在许多应用程序中更加标准/有用(例如:剪切、复制、粘贴、全选)。

Ctrl+Alt 相当安全,但需要用户做更多工作。

我倾向于只是尝试坚持使用 ALT 快捷键,IE 不会固执地坚持处理。

CTRL+A/CTRL+F取消成功演示:
http://jsfiddle.net/egJyT/

This 答案似乎暗示在不放置 IE 的情况下不可能禁用菜单快捷方式进入信息亭模式。

I actually had a web app working just fine with CTRL shortcut keys, but then decided I'd be clever and use the accesskey attribute, and ran into this exact issue with IE.

The problem with going to CTRL shortcut keys is that many of those are more standard/useful across many applications (eg: cut, copy, paste, select all).

Ctrl+Alt is fairly safe, but requires more work on the user's part.

I tend to just try to stick to ALT shortcuts IE doesn't stubbornly insist on handling.

Demo of CTRL + A/CTRL + F being cancelled successfully:
http://jsfiddle.net/egJyT/

This answer seems to imply it isn't possible to disable the menu shortcuts without putting IE into kiosk mode.

弥繁 2024-12-18 20:43:05

请注意,如果您成功阻止浏览器检测到组合键,则可能会导致某些用户无法使用您的页面。许多屏幕阅读器都保留了您能想到的几乎所有键来控制屏幕阅读器,如果在添加快捷键代码之前可以使用屏幕阅读器访问您的页面,那么在添加快捷键代码后,需要屏幕阅读器的用户可能完全无法访问该页面。

阅读这篇关于访问键的文章(有点旧,但可能仍然相关),以及这篇关于保留击键组合的文章,然后再为此投入太多时间问题。

Beware that if you manage to successfully prevent the browser from detecting a key combination you may make your page unusable for some users. Many screen readers have reserved almost any key you can think of to control the screen reader and if your page was accessible using a screen reader before you added the shortcut key code, it may be completely un-accessible users needing screen readers after you add it.

Read this article about access keys (a bit old but probably still relevant), and this article about Reserved Keystroke Combinations before you invest too much time on this problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文