"Scroll" to the very right of a long text input
I have an image selector that allows to choose an image from a gallery, then fills in the URL into a <input type="text">
field. The URLs can be awfully long, and always seeing the first half of the URL in the text field has very little informational value.
Does somebody know a way to "scroll" to the very right of the text field so that the end of the URL is visible instead of the beginning? Without resorting to a textarea.
All browsers except IE6-8/Opera
Set HTMLInputElement.setSelectionRange()
to the length of the input value after explicitly setting the focus()
. The disadvantage is that it scrolls back to start once blurred.
var foo = document.getElementById("foo");
foo.value = "http://stackoverflow.com/questions/1962168/scroll-to-the-very-right-of-a-long-text-input";
foo.focus();
foo.setSelectionRange(foo.value.length,foo.value.length);
<input id="foo">
Lucman Abdulrachman的回答是最好的,但我无法赞成它,所以我在不使用jQuery的情况下添加了另一个解决方案:
var elem = document.getElementById('myInput');
elem.focus();
elem.scrollLeft = elem.scrollWidth;
$('#myInput').get(0).scrollLeft = $('#myInput').get(0).scrollWidth;
链接地址: http://www.djcxy.com/p/68446.html
上一篇: 使用JavaScript将光标置于文本输入元素中的文本末尾
下一篇: “滚动”到长文本输入的最右侧