如何使用 jQuery 检查在单击事件期间是否按下了某个键? [英] How can I check if a key is pressed during the click event with jQuery?

查看:26
本文介绍了如何使用 jQuery 检查在单击事件期间是否按下了某个键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 jQuery 捕捉点击事件,并且能够判断是否同时按下了某个键,以便我可以根据按键事件在回调函数中分叉.

I would like to catch a click event with jQuery and be able to tell if a key was pressed at the same time so I can fork within the callback function based on the keypress event.

例如:

$("button").click(function() {
    if([KEYPRESSED WHILE CLICKED]) {
        // Do something...
    } else {
        // Do something different...
    }
});

这完全可能吗,或者如果可能的话怎么做?

Is this possible at all or how can it be done if it is possible?

推荐答案

您可以轻松地从事件属性中检测 shift、alt 和 control 键;

You can easily detect the shift, alt and control keys from the event properties;

$("button").click(function(evt) {
  if (evt.ctrlKey)
    alert('Ctrl down');
  if (evt.altKey)
    alert('Alt down');
  // ...
});

有关更多属性,请参阅 quirksmode.如果要检测其他键,请参阅 cletus 的回答.

See quirksmode for more properties. If you want to detect other keys, see cletus's answer.

这篇关于如何使用 jQuery 检查在单击事件期间是否按下了某个键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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