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

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

问题描述

我想用jQuery捕获click事件,并能够判断是否同时按下了一个键,这样我就可以基于keypress事件在回调函数中进行分叉.

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 .如果要检测其他键,请参见

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

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

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