javascript - jQuery 捕获按下某个键时的鼠标移动

标签 javascript jquery jquery-events

如果假设按下 H 键并且鼠标在某个 div 上移动,我将 try catch 事件。

KeyPressedmouseMoving 都必须为 true。

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

最佳答案

这可能对您有帮助。

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

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

关于javascript - jQuery 捕获按下某个键时的鼠标移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35790752/

相关文章:

javascript - 带有自定义 URL 的 facebook "apprequests"?

javascript - 如何绑定(bind)到输入字段的浏览器更改? (jQuery)

jquery - 找到输入的onClick事件并替换Value属性

jquery - 将字体大小保存到 session 中

javascript - 如何将点击传播到光标下的所有 div?

javascript - 使用委托(delegate)事件动态禁用/启用 jquery 点击处理程序

javascript - 像真正的浏览器一样用 phantomjs 打开标签页

javascript - 如何在javascript中使用回调函数添加按钮单击事件作为window.confirm

javascript - 如何在 JSP 中对对象列表进行排序

javascript - PHP 使用选择值设置变量