Cursor Positioning in ContentEditable Div

2019-08-10 07:18发布

问题:

I have a contenteditable div where I catch certain words and wrap it with a span. And I try to do this while the user is typing.

Currently, I update the html content of the div by jQuery:

$(div).html($(div).html().replace(....))

However, the moment this is run, the cursor position in the div returns to the very first line.

Is there a different method in updating the contents of the contenteditable without losing the cursor position?