首页 > 将input中的光标移动到文字的末尾后,怎么用js显示光标当前的位置?

将input中的光标移动到文字的末尾后,怎么用js显示光标当前的位置?

一开始的效果是这样的:

我点击了之后,修改昵称,那么会出现一个input的框让用户来填写:

问题:我想在点击了之后,光标移动到input文字最后的位置,可是我看到不到光标的位置,我想让它变成这样,怎么实现?


var $Val = $.trim($(this).val())
$(this).val('').focus().val($Val)

这样是你想要的不


正好之前做富文本编辑器接触了一点
document.selection
document.selection.createRange()
网上找的代码
测试chrome正确运行,
关于selection的讲解:http://www.cnblogs.com/rainman/archive/2011/02/27/1966482.html

function moveEnd(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; 
} 
} 

楼主解决了吗?遇到同样的问题,希望可以帮忙一下,邮箱337083229@qq.com,thank you

【热门文章】
【热门文章】