How to set caret/cursor position in a contenteditable div between two divs.

后端 未结 3 619
别跟我提以往
别跟我提以往 2020-12-10 17:36

Consider the following contenteditable div.

bold text
bold text
3条回答
  •  南方客
    南方客 (楼主)
    2020-12-10 17:58

    Another thing you can do is use a Mutation Observer to catch the mutations and then fix them up after the fact. In my use case I was fortunate in that each element had predefined text. When the mutation observer fires, I simply moved changed text to a new text node before or after the element as appropriate. This seems a lot easier than the other options because the observer fires for all changes to character data, and it also has a proper record of all of the changes, unlike say keypress event.

提交回复
热议问题