event.altKey不能在Chrome和IE中工作 [英] event.altKey not working in chrome and IE

查看:104
本文介绍了event.altKey不能在Chrome和IE中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有这样的代码:

 < html>< head> 
< meta http-equiv =content-typecontent =text / html; charset = UTF-8>

< script type =text / javascript>
var p = 0;

函数reset()
{
//一些努力
}

函数代码(e){
e = e || window.event;
return(e.keyCode || e.which);

window.onload = function(){
document.onkeypress = function(e){
var key = code(e);
if(key == 38 || key == 40)
{}
else if(e.altKey)
{
alert('altkey pressed') ;
}
else
{
//另一件事!
}
};
};
< / script>

< / head>
< body style =padding:30px; font-size:30px; font-family:Courier;>
< span onclick =reset(); ACCESSKEY = R >< /跨度>
< / body>
< / html>

它适用于Firefox,但不适用于IE浏览器。将 alt 更改为 shift 它适用于所有浏览器。但正如你可能注意到的,我想用这个快捷键,我认为这是Chrome和IE中的 Alt 键的问题(因为两者都使用 Alt c $ c> accesskey 属性,但Firefox使用 Alt + Shift 作为指导这里)。

那么你们建议我做什么?

解决方案

您无法检测到chrome上的 alt 按钮,因为它启用了窗口菜单,因此您的页面失去焦点并且未调用keypress事件。另一方面,事件keydown被调用,所以你可以毫无问题地使用它。


Assume we have this code:

<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">

<script type="text/javascript">
var p = 0;

function reset()
{
    // some efforts
}

function code(e) {
    e = e || window.event;
    return(e.keyCode || e.which);
}
window.onload = function(){
    document.onkeypress = function(e){
    var key = code(e);      
    if(key == 38 || key == 40)
    {}
    else if(e.altKey)
    {
        alert('altkey pressed');
    }
    else
    {
        // Another THING!
    }
    };
};
</script>

</head>
<body style="padding:30px; font-size:30px;font-family: Courier;">
<span onclick="reset();" accesskey="r"></span>
</body>
</html>

It works great in Firefox but not in IE an Chrome.
When I change alt into shift it works on all browsers. but as you may notice, I wanted to use this for Shortcut key and I think this is a problem of Alt key in Chrome and IE (because both use Alt for accesskey attribute but Firefox uses Alt+Shift as mentoned here).
So guys what you suggest me to do ?

解决方案

You can not detect alt button on chrome because it enables the window menu, so your page lose focus and the event "keypress" is not called. The event "keydown" on the other hand is called so you can use it with no problem.

这篇关于event.altKey不能在Chrome和IE中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆