Contenteditable setCursorPos(elem,pos),pos = getCursorPos(elem)

有没有人有可行的解决方案来获取或设置内置元素的可满足div的光标位置? 我要做的是创建类似twitter的字段,我将在@ sign上插入。 问题在于浏览器与contenteditable属性差不多,所以我不能依赖它们来插入。 所以我的算法是:

  • 当@按下时
  • GetCursorPos
  • 获取div的内容作为一个字符串,将其分为两半
  • 在两半之间插入跨度的字符串
  • 做$(div).html(new_content) - 游标将被放置到开头
  • 将光标移动到old_cursor + span的文本长度
  • 问题2和5.我检查了以下问题:

  • 在contentEditable <div>上设置光标位置
  • 如何在contenteditable元素(div)中设置插入符(光标)位置?
  • 将插入符号位置始终设置为contenteditable div
  • 如何将光标移动到可能实体的结尾
  • contenteditable,在文本末尾设置插入符号(跨浏览器)
  • 还有更多......对于setCursorPos(elem,pos),没有可行的解决方案(FF,Chrome,IE) - 无处不在移动到最后或保存和恢复。 我也得到了CursorPosition,但有时在Chrome中它会给出不正确的结果,所以这个函数也很赞赏!

    非常感谢!

    链接地址: http://www.djcxy.com/p/74329.html

    上一篇: Contenteditable setCursorPos(elem, pos), pos = getCursorPos(elem)

    下一篇: Set caret after creating new element in contenteditable div