これを単純なホットキー機能に使用しようとしました。これは、いくつかのキーのキープレスに反応しますが、指定された ID のボックスで編集すると反応しません。残念ながら、ホットキーは常に無効になっています。私は常にアラート()を受け取ります:(
テキストフィールドは例えばhttp://tyrant.40in.net/kg/news.php?id=160#commentsにあります
テキスト領域内では機能しますが、テキスト領域内か外側かにかかわらず、スクリプトは認識しません (テキスト領域をクリックして外側をクリックしても効果がありません)。
私を助けてください。
また、$('html') の代わりに (!$('#tfta_1 #search')) を選択して、別の方法で実行しようとしました。これにより、これらの ID の範囲外ではホットキーが機能しなくなりました。残念ながら、これはうまくいきませんでした。
編集: js は、解釈を避けるために、crtl、alt、shift かどうかも確認する必要があります
// Hotkeys (listen to keyboard input)
$('html').keypress(
function(event){
// is cursor at the beginning / end of edit box
var textInput = document.getElementById("tfta_1"), val = textInput.value;
var isAtStart = false, isAtEnd = false;
if (typeof textInput.selectionStart == "number") {
// Non-IE browsers
isAtStart = (textInput.selectionStart == 0);
isAtEnd = (textInput.selectionEnd == val.length);
} else if (document.selection && document.selection.createRange) {
// IE branch
textInput.focus();
var selRange = document.selection.createRange();
var inputRange = textInput.createTextRange();
var inputSelRange = inputRange.duplicate();
inputSelRange.moveToBookmark(selRange.getBookmark());
isAtStart = inputSelRange.compareEndPoints("StartToStart", inputRange) == 0;
isAtEnd = inputSelRange.compareEndPoints("EndToEnd", inputRange) == 0;
}
// combine information -> is cursor in edit box?
var eb = isAtStart + isAtEnd;
// if in comment box
if ( eb ) {
// do nothing
alert('You are in the comment box');
}
// if key 'p' is pressed
else if (event.which == 112){
// open profile page
window.location = home + 'profile.php';
}
// if key 'q' is pressed
else if (event.which == 113){
// open quests overview
window.location = home + 'quests.php';
}
// if key 'r' is pressed
else if (event.which == 114){
// open raids overview
window.location = home + 'raids.php';
}
// if key 'f' is pressed
else if (event.which == 102){
// open fraction tracker
window.location = home + 'factiontracker.php';
}
}
);