本文介绍了用jQuery Shift + mouseover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图检测当光标移动到特定元素上时是否按下Shift键。该功能激活,但只有 之后,我首先点击另一个元素。有没有办法解决这个问题?我已经尝试将焦点设置为文档和元素,并尝试创建一个伪点击功能,但迄今为止没有任何工作。
I'm trying to detect whether the shift key is being pressed while the cursor is moved over a particular element. The function fires, but only after I click on another element first. Is there some way to work around this? I've tried setting focus to both the document and element, and tried creating a pseudo-click function but so far nothing has worked.
例如,以下代码工作只有在我点击页面上的另一个元素后:
For example, the following code works only after I click another element on the page:
$("#selector").mouseover(function(e){
if(e.shiftKey) {
console.log("the shift key is pressed");
}
});
提前感谢任何信息。
推荐答案
在按键事件上检查:
$(document).keypress(function (e) {
if(e.shiftKey) {
pressed = true; // pressed is a global varialbe. Be carefull of the scope
}
}
然后在keyup上:
$(document).keyup(function(event){
pressed = false;
});
然后执行:
$("#selector").mouseover(function(e){
if(pressed) {
console.log("the shift key is pressed");
}
});
或其他方式:
or the other way around :
$("#selector").mouseover(function(e){
isover = true;
});
和
$(document).keypress(function (e) {
if(e.shiftKey) {
alert("do something")
}
}
这篇关于用jQuery Shift + mouseover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!