Javascript textarea undo redo

后端 未结 2 862
醉话见心
醉话见心 2020-12-01 06:41

I\'m making a small javascript editor (for a chrome extension) somewhat like the one on SO.

There\'s a toolbar for manipulation of the text in the textarea. (e.g. su

2条回答
  •  眼角桃花
    2020-12-01 07:18

    If the textarea has focus and its caret is at the correct position,

    document.execCommand("insertText", false, "the text to insert");
    

    will insert the text "the text to insert", preserving the browser's native undo stack. (See the work in progress HTML Editing API spec.) Chrome 18 supports this, but I'm unsure of the exact version it was introduced.

提交回复
热议问题