问题描述
是否可以使用javascript修改contenteditable的元素,以便撤消仍然有效?
Is there a way to modify a contenteditable's elements using javascript so that undo still works?
Hallo 似乎可以做到这一点,请在选择一些文本后尝试单击加粗按钮,我摸索了它的来源,却不知道他们是怎么做的,唯一提到的是 halloreundo
,这是一些gui工具栏.
Hallo appears to be able to do this fine, try clicking the bold button after selecting some text, I grepped through it's source and have no idea how they do it, the only mention is in halloreundo
which is some gui toolbar.
我查看了 undo.js ,但这只是将html保存在数组中,确实限制了撤消堆栈的大小,因此,如果可能的话,我正在寻求本机解决方案.
I've looked at undo.js but that simply saves the html in an array, which would really limit the size of the undo stack, so I'm after a native solution, if possible.
推荐答案
您可以通过 document.execCommand()
,而不是直接进行DOM操作.
You can ensure undo-ability of your edit operations by doing them via document.execCommand()
instead of direct DOM manipulations.
查看显示 bold 命令(当然不能撤消)的此迷你演示: http://jsfiddle.net/qL6Lpy0c/
Check this mini demo that shows the bold command (undoable ofcourse): http://jsfiddle.net/qL6Lpy0c/
这篇关于修改dom后允许内容编辑者撤消的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!