3
我發現這對一個不同的問題:setSelectionRange在瀏覽器中的行爲方式不同?
setCaretToPos = function(input, selectionStart, selectionEnd){
if(input.setSelectionRange){
input.focus();
input.setSelectionRange(selectionStart, selectionEnd);
}else if(input.createTextRange){
var range = input.createTextRange();
range.collapse(true);
range.moveEnd('character', selectionEnd);
range.moveStart('character', selectionStart);
range.select();
}
};
setCaretToPos(8, 12);
應該從8字符和12字符之間的文本區域中選擇文本。
它適用於Firefox和Chrome,但在Opera中,我得到了錯誤的選擇。偏移移動兩個字符後面
它有什麼問題?
看來它與新行有關:
\n
,因爲如果文本不包含新行字符,則選擇是正確的。
我看到它是你缺少你的函數的輸入參數的方式。它在參數列表中,但你沒有將它傳遞給你的函數。檢查了這一點:http://jsfiddle.net/7Tsx6/並嘗試刪除document.getElementById部分,並嘗試這兩種情況。 –
不,這只是我的問題中的一個錯誤,忘記添加輸入參數。問題出現在textarea有新行 – Alex
基於上面的評論你是傳遞jQuery對象還是實際的元素? – Seth