无法使用jQuery热键覆盖Firefox中的ctrl + s

Tal*_*lon 4 jquery jquery-plugins jquery-hotkeys

我正在使用jQuery Hotkeys插件:http://code.google.com/p/js-hotkeys/

这是我正在使用的代码:

$(document).bind('keydown', 'Ctrl+s', function(event) { alert('saving?'); return false; });
Run Code Online (Sandbox Code Playgroud)

在Chrome中,它工作正常,Ctrl + s默认功能被覆盖,但在Firefox中,它会触发警报,并且还会尝试保存html页面.

我知道必须要有它才能使它工作,F​​irefox中的Wordpress让你按ctrl + s保存.

有任何想法吗?

Fab*_*tté 9

好像Firefox中的一个错误alert会破坏代码的同步性.延迟警报似乎解决了这个问题:

$(document).bind('keydown', 'Ctrl+s', function(event) {
  setTimeout(function() {
    alert('saving?');
  }, 0);
  return false;
});
Run Code Online (Sandbox Code Playgroud)

JSbin


这是一个测试用例来证明我的bug声明.

$(document).bind('keydown', 'Ctrl+s', function(event) {
  event.preventDefault();
});
Run Code Online (Sandbox Code Playgroud)

上面的(bin)将很好地阻止保存对话框.现在,如果你之前或之后将其添加警报,保存对话框如果你仍然出现event.preventDefault()event.stopImmediatePropagation()return false:

$(document).bind('keydown', 'Ctrl+s', function(event) {
  event.preventDefault();
  event.stopImmediatePropagation();
  alert('saving?');
  return false;
});
Run Code Online (Sandbox Code Playgroud)

箱子

event.preventDefault()如果没有alerts,它本身足以阻止保存对话框,现在有一个警报可以阻止默认操作.

  • 是的,你是对的,看起来预防需要成为第一件事. (2认同)
  • 很酷的一个家伙.帮助了我很多.谢谢 :) (2认同)