jQuery:如何捕捉按键+鼠标点击事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3872498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 16:15:45  来源:igfitidea点击:

jQuery: How to catch keydown + mouse click event?

keypresskeydownjquery

提问by SaltLake

I have a divelement. I need to catch a mouse click on this divwhile alt-key (keyCode = 17) is pressed.

我有一个div元素。我需要在按下divalt-key (keyCode = 17) 时捕捉鼠标点击。

Here is what i've got to catch key press:

这是我必须抓住的按键:

// Html
<div id="targetDiv">I want to put a ding in the universe.</div>
// Java-script
$(document).ready(function(){
    $(window).bind('keydown', function(event){
        if ( 17 == event.keyCode ) {
           // How to catch mouse click on $('#targetDiv') ?
        }
    });
});

How to catch mouse click on div while alt-key is pressed?

按下alt键时如何捕捉鼠标点击div?

回答by Nick Craver

You can check the .altKeyproperty, for example:

您可以检查.altKey属性,例如:

$(function() {
  $("#targetDiv").click(function(event) {
    if (event.altKey) {
       //do something, alt was down when clicked
    }
  });
});

You can test it out here.

你可以在这里测试一下