无法使用jQuery热键覆盖Firefox中的Ctrl + [英] Can't override ctrl+s in Firefox using jQuery Hotkeys

查看:116
本文介绍了无法使用jQuery热键覆盖Firefox中的Ctrl +的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the jQuery Hotkeys plugin: http://code.google.com/p/js-hotkeys/

这是我正在使用的代码:

Here is the code i'm using:

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

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

In Chrome it works fine and the Ctrl+s default functionality is over-ridden, but in Firefox it fires the alert and it also tries to save the html page.

我知道必须有某种方法才能使它正常工作,Firefox中的Wordpress让您按ctrl + s进行保存.

I know there has to be someway to get it to work, Wordpress in Firefox let's you press ctrl+s to save.

有什么想法吗?

推荐答案

似乎是Firefox中的错误,其中alert破坏了代码的同步性.延迟警报似乎可以解决此问题:

Seems like a bug in Firefox where alert breaks the synchronicity of your code. Delaying the alert seems to workaround the issue:

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

JSbin

这是一个测试用例,可以证明我的错误声明.

Here's a test case to prove my bug claim.

$(document).bind('keydown', 'Ctrl+s', function(event) {
  event.preventDefault();
});

以上( bin )将很好地阻止保存对话框.现在,如果您在警报之前或之后添加警报,则执行event.preventDefault()event.stopImmediatePropagation()return false时,仍将显示保存对话框 :

The above (bin) will prevent the save dialog nicely. Now if you add an alert either before or after it, the save dialog will appear nevertheless if you do event.preventDefault() and event.stopImmediatePropagation() or return false:

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

Bin

event.preventDefault()本身就足以防止没有alert的保存对话框,现在有了警告,可以阻止默认操作.

event.preventDefault() on its own is enough to prevent the save dialog if there are no alerts, now with an alert it is possible to prevent the default action.

这篇关于无法使用jQuery热键覆盖Firefox中的Ctrl +的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆