如何替换浏览器短键默认值

问题描述 投票:2回答:2

根据一些questions我发现可以如何防止组合键,但我想定义一个短键...

一种方法是在jQuery中使用bind()函数,问题是,键默认作业仍然有效。即:Ctrl+s = savepage

有解决方案吗

javascript jquery browser dom-events
2个回答
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;
    }
});

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

在Chrome,Firefox和IE中测试过。


0
投票

请确保绑定到快捷方式的事件包含preventDefault。在jquery中,这应该是这样的:

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

使用其他框架,这可能是不同的,如e.stop()e.stopEvent()

© www.soinside.com 2019 - 2024. All rights reserved.