0
我的要求是将光标放置在文本框中已经有文本的文本末尾。如何使用Java Script/css/JQuery生成功能。将光标设置为文本框文本的末尾
<span>
<input id="listInput" class="autocomplete-input singleselect-autocomplete-input" type="text" />
</span>
预先感谢您。
我的要求是将光标放置在文本框中已经有文本的文本末尾。如何使用Java Script/css/JQuery生成功能。将光标设置为文本框文本的末尾
<span>
<input id="listInput" class="autocomplete-input singleselect-autocomplete-input" type="text" />
</span>
预先感谢您。
在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();
}
}
您需要将selectionStart 和 selectionEnd属性的textarea设置为当前值的长度。
ü可以请提供代码,它完美的代码,以便在诸如铬,IE,FF,野生动物园所有的浏览器工作 –