2016-07-10 108 views
0

我的要求是將光標放置在文本框中已經有文本的文本末尾。如何使用Java Script/css/JQuery生成功能。將光標設置爲文本框文本的末尾

<span> 
 
    <input id="listInput" class="autocomplete-input singleselect-autocomplete-input" type="text" /> 
 
</span>

預先感謝您。

回答

0

在chrome/IE/FF中使用下面的函數,它適用於我。

$("#inputtextboxID").on("focus", this.SetCaretAtEnd.bind()); 
 

 
function SetCaretAtEnd(){ 
 
var RefValue = $("#inputtextboxID")[0]; 
 
var RefValueLen = RefValue.value.length; 
 
    // For IE Only 
 
    if (document.selection) { 
 
     // Set focus 
 
     RefValue.focus(); 
 
     // Use IE Ranges 
 
     var selectedText = document.selection.createRange(); 
 
     // Reset position to 0 & then set at end 
 
     selectedText.moveStart('character', -RefValueLen); 
 
     selectedText.moveStart('character', RefValueLen); 
 
     selectedText.moveEnd('character', 0); 
 
     selectedText.select(); 
 
    } 
 
    else if (RefValue.selectionStart || RefValue.selectionStart == '0') { 
 
     // Firefox/Chrome 
 
     RefValue.selectionStart = RefValueLen; 
 
     RefValue.selectionEnd = RefValueLen; 
 
     RefValue.focus(); 
 
    } 
 
    }

0

您需要將selectionStart selectionEnd屬性的textarea設置爲當前值的長度。

+0

ü可以請提供代碼,它完美的代碼,以便在諸如鉻,IE,FF,野生動物園所有的瀏覽器工作 –

相關問題