FireFoxのテキストエリア内の選択項目にスクロールして同様の問題が発生しました。テキストスペースの選択を上書きするため、「スペース」と「バックスペース」の文字を送信できませんでした。だから私は、選択の直後にキャラクターを事実上再タイピングして、選択を目に見えるようにするより良い方法を見つけました。ここで
はコードです:
function setSelRange(inputEl, selStart, selEnd) {
if (inputEl.createTextRange) {
var range = inputEl.createTextRange();
range.collapse(true);
range.moveEnd('character', selEnd);
range.moveStart('character', selStart);
range.select();
//range.scrollIntoView();
} else if (inputEl.setSelectionRange) {
inputEl.focus();
inputEl.setSelectionRange(selEnd, selEnd + 1);
// ---- Firefox Workaround ----
// Send a virtual key, which is the character immediately after the
// selected text. It justs rewrites the same character so that no unnecessary changes
// are made to the content.
// When the selection is at the end of the textarea, an extra space is appended
// because the inputEl.value.charCodeAt(selEnd) would otherwise cause an error.
var evt = document.createEvent("KeyboardEvent");
if (inputEl.value.length == selEnd) {
evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, 32);
} else {
evt.initKeyEvent("keypress", true, true, null, false, false, false, false, 0, inputEl.value.charCodeAt(selEnd));
}
inputEl.dispatchEvent(evt);
inputEl.setSelectionRange(selStart, selEnd);
}
}
がこの探している人にお役に立てば幸いです。私はこれを理解しようと多くの時間を無駄にした。