I just use js to set an text input's value. But when the text is longer than input can hold, the excess text is hidden in the right part of the input. How can I hide the excess text in left area just like the normal typing? Forgive my poor english.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
When you perform this your caret position is at the beginning of the text. You need to move the caret position to the end.
function setCursor(node,pos){
var node = (typeof node == "string" || node instanceof String) ? document.getElementById(node) : node;
if (!node) {
return false;
} else if (node.createTextRange) {
var textRange = node.createTextRange();
textRange.collapse(true);
textRange.moveEnd(pos);
textRange.moveStart(pos);
textRange.select();
return true;
} else if (node.setSelectionRange) {
node.setSelectionRange(pos,pos);
return true;
}
return false;
}
setCursor(input, input.value.length); // input is your textbox