How to replace browser short keys defaults

僤鯓⒐⒋嵵緔 提交于 2019-12-07 22:36:13

问题


According to some questions I find can out how to prevent a key combination, but I want to define a short key...

One way is to use bind() function in jQuery, and the problem is, keys default job is still working. i.e: Ctrl+s = savepage

Any solution?


回答1:


The question you linked isn't exactly what you were looking for, but the same concept can be used. Here's the jQuery equivalent only listening for 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;
    }
});

http://jsfiddle.net/SEQVD/3/

Tested in Chrome, Firefox, and IE.




回答2:


Please ensure that the events you've bind to a shortcut contain a preventDefault. In jquery this should look like:

function event(e) {
  e.preventDefault();
}

Using other frameworks this could be different like e.stop() or e.stopEvent().



来源:https://stackoverflow.com/questions/15205150/how-to-replace-browser-short-keys-defaults

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!