使用jQuery热键无法在Firefox中覆盖ctrl + s
我正在使用jQuery Hotkeys插件: http : //code.google.com/p/js-hotkeys/
这是我正在使用的代码:
$(document).bind('keydown', 'Ctrl+s', function(event) { alert('saving?'); return false; });
在Chrome中,它工作正常,Ctrl + s默认function被覆盖,但在Firefox中,它会触发警报,并且还会尝试保存html页面。
我知道必须要有它才能使它工作,Firefox中的Wordpress让你按ctrl + s保存。
有任何想法吗?
看起来像Firefox中的一个错误,其中alert
会破坏代码的同步性。 延迟警报似乎解决了这个问题:
$(document).bind('keydown', 'Ctrl+s', function(event) { setTimeout(function() { alert('saving?'); }, 0); return false; });
JSbin
这是一个测试案例来certificate我的bug索赔。
$(document).bind('keydown', 'Ctrl+s', function(event) { event.preventDefault(); });
上面的( bin )将很好地阻止保存对话框。 现在,如果您在其之前或之后添加警报,那么如果您执行event.preventDefault()
和event.stopImmediatePropagation()
或return false
,则仍会显示保存对话框:
$(document).bind('keydown', 'Ctrl+s', function(event) { event.preventDefault(); event.stopImmediatePropagation(); alert('saving?'); return false; });
箱子
如果没有alert
, event.preventDefault()
本身足以阻止保存对话框,现在有警报可以阻止默认操作。
这对我有用: