3

I'd like to restrict the jQuery Click Event.

The Problem: if you keydown whatever key... and click with the mouse: click is fired.

For a JS game dev., I'd prefer:

If whatever key is down... and you click: click is NOT fired

How to prepare un $.event.special.click for a absolutely and strictly "ONLY-mouse" click ?

4

1 回答 1

4

这可以简单地通过使用信号量来完成,例如旋转锁。

这是通过在按下 keydown 时增加锁来实现的。当 keyup 发生时,将该锁设置为 0。在单击事件中,测试锁是否为 0。如果不是,则不要发出单击操作。

var SpinningLock = 0;

$('#target').keydown(function() {
  SpinningLock++;
});

$('#target').keyup(function() {
  SpinningLock = 0;
});

$('#target').click(function() {
  if( SpinningLock > 0) return;
  //TODO: implement click action
});
于 2012-07-30T00:04:37.660 回答