使用jQuery切换+鼠标悬停

Col*_*ock 10 methods jquery events

我正在尝试检测光标移动到特定元素上时是否按下了shift键.该函数触发,但只有我先点击另一个元素之后才会触发.有办法解决这个问题吗?我已经尝试将焦点设置到文档和元素,并尝试创建一个伪点击功能,但到目前为止没有任何工作.

例如,以下代码仅在我单击页面上的另一个元素后才起作用:

$("#selector").mouseover(function(e){
    if(e.shiftKey) {
        console.log("the shift key is pressed");
    }
});
Run Code Online (Sandbox Code Playgroud)

提前感谢您提供任何信息.

mar*_*cgg 10

在keypress事件上检查这个:

$(document).keypress(function (e) {

  if(e.shiftKey) {
    pressed = true; // pressed is a global varialbe. Be carefull of the scope
  }

}
Run Code Online (Sandbox Code Playgroud)

然后在键盘上:

$(document).keyup(function(event){
   pressed = false;
});
Run Code Online (Sandbox Code Playgroud)

然后做:

$("#selector").mouseover(function(e){
    if(pressed) {
        console.log("the shift key is pressed");
    }
});
Run Code Online (Sandbox Code Playgroud)

或者相反:

$("#selector").mouseover(function(e){
    isover = true;
});
Run Code Online (Sandbox Code Playgroud)

   $(document).keypress(function (e) {

      if(e.shiftKey) {
        alert("do something")
      }

   }
Run Code Online (Sandbox Code Playgroud)


Mar*_*aer 8

当按下和释放换档键时,不必存储在变量中.你可以实现你想要的东西:

$('#selector').mouseover(
    function(e){
        if (e.shiftKey)
        {
            console.log("the shift key is pressed");
        }
    }
);
Run Code Online (Sandbox Code Playgroud)