Html5在ios上input标签自动填充内容移动光标至尾的方法

时间:2021-12-12 18:12:41

在做ionic框架下开发webapp,遇到了input框无法对于自动填充的内容做到聚焦时将光标自动移到末尾。在调研学习后,发现了以下方法,希望给大家有所帮助:

moveEnd: function(obj) {
obj.focus();
var len = obj.value.length;

if (document.selection) {
var sel = obj.createTextRange();

sel.moveStart('character', len);
sel.collapse();
sel.select();
} else if (typeof obj.selectionStart === 'number' && typeof obj.selectionEnd === 'number') {
obj.selectionStart = obj.selectionEnd = len;
}
},

在serves中实现以下方法,然后在controller.js中实现ng-click的绑定事件:

$scope.textAreaMoveEnd = function(){
console.log("点击事件触发");
commentService.moveEnd(document.getElementById('task-detail-comment-area'));
}

记得将input或者textarea的ID写为选择器里的id;