jquery 如何获得光标的位置 ,并且把光标定位到最后?

2013-01-28 11:15:40 +08:00
 inyfee
有一个div contenteditable
现在动态设置内容进去之后,光标会被定位到最前面,这不是正常人想要的,希望能定位到最后。
我在网上找到这段代码 :
$.fn.focusEnd = function(txtareaID){
var id = $("#"+txtareaID);
id.focus();
console.log(id.setSelectionRange);
if (id.setSelectionRange) {
id.focus();
id.setSelectionRange(start,end);
}
else if (id.createTextRange) {
var range = id.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
}

但是都没效果:
console.log(id.setSelectionRange)
console.log(id.createTextRange)
在chrome 上显示 :undefined
请问这是怎么回事,该怎么样处理呢?谢谢
5010 次点击
所在节点    问与答
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/59006

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX