[英]Prevent default event action not working…?
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. 但是,我尝试使用Alt + X快捷方式遇到了一个小问题。 The event runs just fine and returns false
as it should, but the browser's File menu comes up regardless. 该事件运行得很好并且应该返回false
,但浏览器的File菜单无论如何都会出现。 I've also tried the preventDefault
method, but no change. 我也尝试过preventDefault
方法,但没有改变。
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. 如上所述,覆盖F5键的默认操作可以正常工作 - 只有在没有iframe处于活动状态时,浏览器才会重新加载页面。 I don't quite see how to prevent the menu from appearing when Alt+X is pressed. 我不太清楚如何在按下Alt + X时阻止显示菜单。
use stopPropagation(e);
使用stopPropagation(e);
instead of preventDefault
method 而不是preventDefault
方法
function stopPropagation(e)
{
e = e || event;/* get IE event ( not passed ) */
e.stopPropagation? e.stopPropagation() : e.cancelBubble = true;
}
Another SO question which mentions that preventDefault has issue in IE. 另一个SO问题,提到preventDefault在IE中有问题。
Try using below code as per MSDN Reference 根据MSDN参考,尝试使用以下代码
event.returnValue=false;
And some point from Detecting keystrokes 还有一点来自于检测击键
Some general caveats: 一些一般性的警告:
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快捷键工作正常,但后来决定我是聪明的并使用accesskey
属性,并遇到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快捷键的问题在于许多应用程序中的许多应用程序更加标准/有用(例如:剪切,复制,粘贴,全选)。
Ctrl+Alt is fairly safe, but requires more work on the user's part. Ctrl + Alt相当安全,但需要用户更多的工作。
I tend to just try to stick to ALT shortcuts IE doesn't stubbornly insist on handling. 我倾向于只是试图坚持ALT快捷方式IE并不是固执地坚持处理。
Demo of CTRL + A/CTRL + F being cancelled successfully: http://jsfiddle.net/egJyT/ CTRL + A / CTRL + F演示成功取消: http : //jsfiddle.net/egJyT/
This answer seems to imply it isn't possible to disable the menu shortcuts without putting IE into kiosk mode. 这个答案似乎意味着,如果不将IE放入kiosk模式,就无法禁用菜单快捷方式。
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. 阅读本文关于访问密钥 (有点旧但可能仍然相关),以及关于预留键击组合的文章,然后再花费太多时间来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.