将光标移动到输入的末尾

cyb*_*_ua 0 html javascript

我的 input 和 setSelectionRange 方法有问题。问题是我在最后设置了光标位置:

input.setSelectionRange(input.value.length,input.value.length );
Run Code Online (Sandbox Code Playgroud)

光标移动到结尾,但文本保持在同一位置。它适用于 Firefox,但不适用于 Chrome。

input.setSelectionRange(input.value.length,input.value.length );
Run Code Online (Sandbox Code Playgroud)
var input = document.querySelector("input");
input.onclick = function (){
  this.setSelectionRange(this.value.length,this.value.length )
}
Run Code Online (Sandbox Code Playgroud)

cyb*_*_ua 5

我找到了简单的解决方案https://jsfiddle.net/r3yohhc0/1/,属性 scrollLeft

input.scrollLeft = input.scrollWidth;
Run Code Online (Sandbox Code Playgroud)