Handling #39;ctrl+s#39; keypress event for browser(处理浏览器的“ctrl+s按键事件)
问题描述
我试图为基于浏览器的应用程序实现 CTRL+S 功能.我进行了搜索,并在以下问题中遇到了两个脚本
I was trying to implement the CTRL+S feature for a browser based application. I made a search and came across two scripts in the following to questions
捕获 CTRL+S 的最佳跨浏览器方法jQuery?
Ctrl+S preventDefault 在 Chrome 中
但是,当我尝试实现它时,它工作了,但我仍然得到默认浏览器保存对话框/窗口.
However, when I tried to implement it, it worked but, I still get the default browser save dialog box/window.
我的代码:对于 shortcut.js
:
shortcut.add("Ctrl+S",function() {
alert("Hi there!");
},
{
'type':'keydown',
'propagate':false,
'target':document
});
jQuery 热键.js:
jQuery hotkeys.js:
$(document).bind('keydown', 'ctrl+s', function(e) {
e.preventDefault();
alert('Ctrl+S');
return false;
});
我相信 e.preventDefault();
应该可以解决问题,但由于某种原因它不起作用.我哪里错了.对不起,如果很简单,还在学习jJvascript.
I believe e.preventDefault();
should do the trick, but for some reason it doesn't work. Where am I going wrong.Sorry if it is simple, still learning jJvascript.
推荐答案
这只是为我使用的问题添加不同的实现.改编自 SO 答案.也适用于 MAC
This is to just add a different implementation to the question used by me. Adapted from a SO answer.Also,works for MAC
document.addEventListener("keydown", function(e) {
if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
e.preventDefault();
//your implementation or function calls
}
}, false);
这篇关于处理浏览器的“ctrl+s"按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:处理浏览器的“ctrl+s"按键事件
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01