cod*_*ner 8 javascript jquery event-handling javascript-events
我正在尝试为基于浏览器的应用程序实现CTRL+ S功能.我进行了搜索并在下面遇到了两个问题的脚本
使用JQuery捕获CTRL + S的最佳跨浏览器方法?
在Chrome中按Ctrl + S preventDefault
但是,当我尝试实现它时,它工作但是,我仍然得到默认的浏览器保存对话框/窗口.
我的代码:For shortcut.js
shortcut.add("Ctrl+S",function() {
alert("Hi there!");
},
{
'type':'keydown',
'propagate':false,
'target':document
});
Run Code Online (Sandbox Code Playgroud)
jquery hotkeys.js
$(document).bind('keydown', 'ctrl+s', function(e) {
e.preventDefault();
alert('Ctrl+S');
return false;
});
Run Code Online (Sandbox Code Playgroud)
我相信e.preventDefault();应该做的伎俩,但由于某种原因它不起作用.我在哪里出错.如果它很简单,仍然学习javascript.
感谢您的时间.
Cer*_*rus 21
你不需要任何这些库,只需试试这个:
$(document).on('keydown', function(e){
if(e.ctrlKey && e.which === 83){ // Check for the Ctrl key being pressed, and if the key = [S] (83)
console.log('Ctrl+S!');
e.preventDefault();
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
问题是您的代码停止了alert(),阻止您的功能中断保存对话框.
(仍然使用jQuery)
cod*_*ner 14
这只是为我使用的问题添加不同的实现. 改编自SO答案.也适用于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);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13800 次 |
| 最近记录: |