防止默认事件操作不起作用? [英] Prevent default event action not working...?
问题描述
我想在我的网站上添加键盘快捷键,以便使用键盘快速导航。我遇到一个小问题,但是,我尝试的Alt + X快捷方式。事件运行正常,并返回 false
,因为它应该,但浏览器的文件菜单出现不管。我也尝试过 preventDefault
方法,但没有变化。
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.
脚本的剪切版本是:
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时防止菜单出现。
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.
推荐答案
我实际上有一个网页应用程序使用CTRL快捷键正常工作,但后来决定我会聪明,并使用 accesskey
属性,并遇到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.
进入CTRL快捷键的问题是,许多应用程序中的许多应用程序(例如:剪切,复制,粘贴,选择所有)更为标准/有用。
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是相当安全的,但需要更多的工作在用户的部分。
Ctrl+Alt is fairly safe, but requires more work on the user's part.
我倾向于尝试坚持ALT快捷方式IE不固执地坚持处理。
I tend to just try to stick to ALT shortcuts IE doesn't stubbornly insist on handling.
CTRL + A / CTRL + F的演示被成功取消:
http://jsfiddle.net/egJyT/
Demo of CTRL + A/CTRL + F being cancelled successfully: http://jsfiddle.net/egJyT/
这个答案似乎暗示不可能禁用菜单快捷方式,而不会将IE放入信息亭模式。
This answer seems to imply it isn't possible to disable the menu shortcuts without putting IE into kiosk mode.
这篇关于防止默认事件操作不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!