Chrome中的Ctrl + S preventDefault [英] Ctrl+S preventDefault in Chrome

查看:137
本文介绍了Chrome中的Ctrl + S preventDefault的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Chrome中抓住 Ctrl + S ,并阻止默认浏览器行为保存页面。如何?



(只需发布问题和答案,因为我在此之后很长一段时间并没有找到解决方案)$ b $据我所知,秘诀在于 Ctrl + S 不是NOT触发按键事件,只有keydown事件。



使用 jQuery.hotkeys

  $(document).bind('keydown','ctrl + s' ,函数(e){
e.preventDefault();
alert('Ctrl + S');
return false;
});

仅限jQuery:

  $(document).bind('keydown',function(e){
if(e.ctrlKey&&(e.which == 83)){
e.preventDefault();
alert('Ctrl + S');
return false;
}
});

编辑2012.12.17 - jQuery.hotkeys说


如果您位于输入元素的内部,则不会跟踪热键(除非
将热键直接绑定到输入)。这有助于避免
与正常用户输入的冲突。



I Want to catch Ctrl+S in Chrome, and prevent the default browser behavior to save the page. How?

(Just posting the question & answer as I was after this for a pretty long time and didn't find a solution)

解决方案

As far as I can see, the secret sauce is, that Ctrl+S does NOT fire the keypress event, only the keydown event.

Using jQuery.hotkeys:

$(document).bind('keydown', 'ctrl+s', function(e) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
});

Only with jQuery:

$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
  }
});

Edit 2012.12.17 - jQuery.hotkeys says

Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

这篇关于Chrome中的Ctrl + S preventDefault的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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