我找到了解決問題的辦法。它使用普通的舊Javascript完成,並應與大多數瀏覽器兼容。如果沒有,請隨時舉報。
/**
* addKeyActions takes an array of objects that looks like this:
*
* {
* keyCodes: [120], //F9
* modifiers: ['ctrlKey', 'shiftKey', 'altKey', 'altGraphKey', 'metaKey'], //Only legal modifiers
* callback: function(event){};
* }
*
* A good resource to find the keycodes you are looking for is:
* http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
*
* However, there are more keycodes than written on this page, so if you can't find what you are looking for,
* I have provided a function that you can call to display the keycode for the current button pressed - alertKeyCodes
*
*
* @Version 1.01
*/
/*
* --HOW TO USE--
*
var allModifiersKeys = {
keyCodes: [120, 121], //F9, F10
modifiers: ['ctrlKey', 'shiftKey', 'altKey'],
callback: function(event){alert("All modifiers pressed + Function-button!");}
};
var altKeys = {
keyCodes: [122, 123], //F11, F12
modifiers: ['altKey'],
callback: function(event){alert("ALT pressed + Function-button!");}
};
var shiftCtrlKeys = {
keyCodes: [116, 117], //F5, F6
modifiers: ['ctrlKey', 'shiftKey'],
callback: function(event){alert("CTRL+SHIFT+Function-button pressed!");}
};
var noModifierKeys = {
keyCodes: [118, 119], //F7, F8
callback: function(event){preventDefaultEventPropagation(); alert("Function-button!");} //Do not forget to also declare the preventDefaultEventPropagation-function
//that is listed below for this example to work
};
addKeyActions([allModifiersKeys, altKeys, shiftCtrlKeys, noModifierKeys]);
*/
function addKeyActions(key){
document.onkeydown = function(){
var matchingKeyCode = findMatchingKeyCode();
var match = matchModifiers(matchingKeyCode);
if(match){
matchingKeyCode.callback(event);
}
};
function findMatchingKeyCode(){
var eventKeyCode = event.keyCode || event.which || event.charCode;
var returnVal;
for(var i=0; i<key.length; i++){
var tempKey = key[i];
var containsKeyCode = isValueInArray(eventKeyCode, tempKey.keyCodes);
if(containsKeyCode){
returnVal = tempKey;
}
}
return returnVal;
}
function isValueInArray(value, array){
var hasValue = false;
for(var i=0; i < array.length; i++){
var tempValue = array[i];
if(tempValue === value){
hasValue = true;
}
}
return hasValue;
}
function matchModifiers(key){
var match = false;
if(key != undefined){
match = true;
if(key.modifiers instanceof Array){
for(var i=0; i<key.modifiers.length; i++){
var modifier = key.modifiers[i];
match = match && event[modifier];
}
}
}
return match;
}
}
/**
* Useful callbacks/functions are listed below
*/
function preventDefaultEventPropagation(){
if(event.cancelBubble) event.cancelBubble = true;
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
event.keyCode = 0;
return false;
}
/**
* Useful methods during developement
*/
function alertKeyCodes(){
document.onkeydown = function(){
alert(event.keyCode || event.which || event.charCode);
if(event.cancelBubble) event.cancelBubble = true;
if(event.stopPropagation) event.stopPropagation();
if(event.preventDefault) event.preventDefault();
event.returnValue = false;
event.keyCode = 0;
return false;
};
}
請注意,函數和使用位置的定義在2個獨立的文件中。一個是從準備好文檔開始的調用以及上面的定義。
$(document).ready(function() {
var SingleKeys = {
keyCodes: [114, 116, 121, 122], //F3, F5, F10, F11
callback: function(event){return preventDefaultEventPropagation();}
};
var BackspaceKeys = {
keyCodes: [8], //Backspace
callback: function(event){
var sourceOfEvent = event.target || event.srcElement;
if(sourceOfEvent.tagName == "BODY"){
return preventDefaultEventPropagation();
}
}
};
var EnterKeys = {
keyCodes: [13], //Enter
callback: function(event){
var sourceOfEvent = event.target || event.srcElement;
if(sourceOfEvent.tagName == "BODY"){
return preventDefaultEventPropagation();
}
if(sourceOfEvent.tagName == "INPUT" && sourceOfEvent.type == "text"){
return preventDefaultEventPropagation();
}
}
};
var AltKeys = {
keyCodes: [36, 37, 39], //Home, Left, Right
modifiers: ['altKey'],
callback: function(event){return preventDefaultEventPropagation();}
};
var CtrlKeys = {
keyCodes: [66, 68, 69, 72, 73, 76, 78, 79, 82], //B, D, E, H, I, L, N, O, R
modifiers: ['ctrlKey'],
callback: function(event){return preventDefaultEventPropagation();}
};
addKeyActions([SingleKeys, BackspaceKeys, EnterKeys, AltKeys, CtrlKeys]);
});
什麼是您的jQuery版本? –
它們全都未能被阻止,或者它是特定的鍵。 –
這可能是一種替代方法:http://stackoverflow.com/questions/1400528/is-there-a-way-to-capture-override-ctrl-r-or-f--on-ie-using-javascript此外,你應該在事件處理程序中返回false。 –