本文介绍了如何同时检测按键和鼠标悬停的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
好的,我可以使用 .on('mouseover')
检测鼠标悬停,我可以检测到按键使用
and I can detect keypresses using
$(document).keypress(function(e) {
console.log(e.which);
}
但是当我按下时如何检测鼠标悬停在哪个图像上?某个按钮?
but how do I detect which image my mouse is hovering over when I press a certain button?
这个想法是能够在将鼠标悬停在d上时删除图像。任何想法?
the idea is to be able to delete an image by pressing d while hovering over it. any ideas ?
推荐答案
您只需切换显示当前正在徘徊的类或数据属性
You can just toggle a class or data-attribute that shows you which one is currently being hovered
$('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
}
});
这篇关于如何同时检测按键和鼠标悬停的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!