jQuery:如何捕获键盘按下+鼠标单击事件?

问题描述:

我有一个div元素.在按下alt键(keyCode = 17)的同时,我需要在此div上单击鼠标.

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

这是我必须抓住按键的地方:

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') ?
        }
    });
});

如何在按住alt键的同时捕捉鼠标在div上的点击?

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

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

You can check the .altKey property, for example:

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

您可以在此处进行测试.