如何同时检测按键和鼠标悬停

The*_*vil 14 javascript jquery keypress mouseover

好的,我可以使用检测鼠标悬停 .on('mouseover')

我可以使用检测按键

$(document).keypress(function(e) {
        console.log(e.which);
}
Run Code Online (Sandbox Code Playgroud)

但是当我按下某个按钮时,如何检测鼠标悬停在哪个图像上?

我的想法是能够在将鼠标悬停在图像上时按d来删除图像.有任何想法吗 ?

ᾠῗᵲ*_*ᵲᄐᶌ 6

您只需切换一个类或数据属性,以显示当前正在悬停的那个

$('img').hover(function(){
   $(this).toggleClass('active'); // if hovered then it has class active
});
$(document).keypress(function(e) {    
    if(e.which == 100){
       $('.active').remove(); // if d is pressed then remove active image
    }
});
Run Code Online (Sandbox Code Playgroud)

FIDDLE