jQuery $(文档).keydown()问题 [英] jQuery $(document).keydown() issues

查看:126
本文介绍了jQuery $(文档).keydown()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究一个需要标准桌面应用程序菜单栏的网络应用程序。

I am currently working on a web app which requires a standard desktop application menu bar. I have started to work on the menu bar and have the layout sorted.

不幸的是,客户端想要访问键(例如Alt + F)加载文件菜单。按下Alt键时,我想显示访问键。我现在有以下jQuery代码来处理这个问题:

Unfortunatrly, the client wants to have access keys (such as Alt+F) loads the "File" menu. When the Alt key is pressed, I want to show the access keys. I currently have the following jQuery code to handle this:

$(document).ready(function() {
    $(this).keydown(function(e) {
        if(e.keyCode == 18) { alt_shifter = true; $('.access_key').css({ textDecoration: 'underline' }); }
    });

    $(this).keyup(function(e) {
        if(e.keyCode == 18) { alt_shifter = false; $('.access_key').css({ textDecoration: 'none' }); }
    });
});

不幸的是,正如您在jsFiddle上看到的,出于某种原因,在Google Chrome中, ,按键被加下划线,然后在按键上标准化。但是,再次按下Alt键不会执行任何操作,直到下一次按下Alt。就好像一旦下划线已经完成并且 keyup 已被触发,它将不会注册该键盘直到再次按下。

Unfortunately, as you'll see on the jsFiddle, for some reason in Google Chrome, when pressing Alt, the keys are underlined, then normalised on keyup. However, pressing Alt again does nothing, until the next time it's pressed. It's almost as if once the underline has been done and keyup has been fired, it does not register the keyup until it's pressed again.

以下是jsFiddle演示> http://jsfiddle.net/Ht2wD/

Here's the jsFiddle demo > http://jsfiddle.net/Ht2wD/

任何帮助最受欢迎!

推荐答案

e.preventDefault(); 恰好在keyCode检查之前,它似乎工作。以下是帮助我找到答案的网站 http://unixpapa.com/js/key.html

I altered the code to include e.preventDefault(); right before the keyCode check and it seemed to work. Here's the website that helped me arrive at that answer http://unixpapa.com/js/key.html

这篇关于jQuery $(文档).keydown()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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