根据一些questions我发现可以如何防止组合键,但我想定义一个短键...
一种方法是在jQuery中使用bind()
函数,问题是,键默认作业仍然有效。即:Ctrl+s = savepage
有解决方案吗
您链接的问题并不完全符合您的要求,但可以使用相同的概念。这是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中测试过。
请确保绑定到快捷方式的事件包含preventDefault。在jquery中,这应该是这样的:
function event(e) {
e.preventDefault();
}
使用其他框架,这可能是不同的,如e.stop()
或e.stopEvent()
。