当按下某个键时,jquery捕获mousemove

时间:2016-03-04 07:53:37

标签: javascript jquery javascript-events

如果按下键'H'并且鼠标移动到某个div上,我试图捕获事件。

KeyPressed和mouseMoving两者都必须为真

https://jsfiddle.net/bababalcksheep/1Loeh2pn/

$('#header').on("mousemove keydown", function(e) {
  //only print if key 'H' is pressed and mouse is moving over $('#header')
  if (e.type === 'mousemove' && e.which === 72) {
    console.log('working');
  }
});

1 个答案:

答案 0 :(得分:1)

这可能对你有所帮助。

https://jsfiddle.net/1Loeh2pn/3/

$(function() {
    $("#header").hover(function() {
        $(document).keydown(function(e) {
            if(e.which == 72){
                console.log('H')
            }
        });
    });
});