处理浏览器的“ctrl + s”按键事件 [英] Handling 'ctrl+s' keypress event for browser
问题描述
我试图为基于浏览器的应用程序实现 ctrl + s
功能。我做了一个搜索,并遇到了两个脚本以下问题
I was trying to implement the ctrl+s
feature for a browser based application.I made a search and came across 2 scripts in the following to questions
使用JQuery捕获CTRL + S的最佳跨浏览器方法 _
Ctrl + S防止在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 hotkeys.js
$(document).bind('keydown', 'ctrl+s', function(e) {
e.preventDefault();
alert('Ctrl+S');
return false;
});
我相信 e.preventDefault();
应该做的伎俩,但由于某种原因,它不工作。我在哪里错了。如果它很简单,仍然学习JavaScript。
谢谢你的时间。
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 javascript.
Thank you for your time.
推荐答案
这是为了给我使用的问题添加一个不同的实现。
从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”按键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!