根据一些questions,我发现如何防止组合键, 但我想定义一个短键......
一种方法是在jQuery中使用bind()
函数,问题是,键默认作业仍然有效。即:Ctrl+s = savepage
任何解决方案?
答案 0 :(得分:1)
您链接的问题并不完全是您所寻找的问题,但可以使用相同的概念。这是jQuery等效的只听取Ctrl + s
$(document).on("keydown",function(e){
if (e.originalEvent.ctrlKey && e.which == 83/*"s"*/) {
setTimeout(function(){ //Fix for firefox. Ref: http://stackoverflow.com/questions/14860759/cant-override-ctrls-in-firefox-using-jquery-hotkeys
// do save stuff...
alert("Saved!"); // if you remove alert, you can remove setTimeout
},0);
return false;
}
});
在Chrome,Firefox和IE中测试过。
答案 1 :(得分:0)
请确保绑定到快捷方式的事件包含preventDefault。在jquery中,这应该是:
function event(e) {
e.preventDefault();
}
使用其他框架可能会有所不同,例如e.stop()
或e.stopEvent()
。