Save / restore selection on contentEditable AFTER modifying innerHTML

前端 未结 1 976
被撕碎了的回忆
被撕碎了的回忆 2021-01-18 02:00

I know getting / setting cursor position in a contentEditable is damn near impossible. I don\'t care about knowing this information. I need to be able to save current select

1条回答
  •  天涯浪人
    2021-01-18 02:20

    If you're doing some kind of string substitution on the existing innerHTML of your editable element, you may be able to use my Rangy library and its save/restore selection module. It uses invisible elements with particular IDs to mark the start and end boundaries of the selection, so if your innerHTML change does not include these elements then this will not work.

    Another alternative is to do it based purely on character indices within the text nodes of the element. I've written a naive implementation here: https://stackoverflow.com/q/5596688/96100

    0 讨论(0)
提交回复
热议问题