最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - textarea: button to trigger native undoredo - Stack Overflow

programmeradmin2浏览0评论

I have a textarea. It has default undo/redo functionality, either with ctrl-z/y or by right-clicking and choosing copy/paste.

I want to create an undo/redo button and trigger the native undo/redo. I'm not sure how to trigger this. (Was surprised that my users don't know ctrl-z)

I have a textarea. It has default undo/redo functionality, either with ctrl-z/y or by right-clicking and choosing copy/paste.

I want to create an undo/redo button and trigger the native undo/redo. I'm not sure how to trigger this. (Was surprised that my users don't know ctrl-z)

Share Improve this question asked Aug 5, 2013 at 9:22 user984003user984003 29.6k69 gold badges202 silver badges315 bronze badges 3
  • 2 possible duplicate of Unable to get document.execCommand('undo') working in the same manner across browsers – NDM Commented Aug 5, 2013 at 9:24
  • That question contains the answer to my question (use execCommand). But finding that question requires already knowing to search for "execCommand", which is what I didn't know. So I still think it could be worthwhile to have this question here with an answer. But whatever others think... – user984003 Commented Aug 5, 2013 at 9:30
  • 1 possible duplicate, does not mean it is a duplicate per se, it's just a flag for moderators to take a look, and for future people landing here to have an extra link to check out. – NDM Commented Aug 5, 2013 at 9:39
Add a ment  | 

2 Answers 2

Reset to default 5

You can use document.execCommand to achieve this functionallity. It is used by some HTML-editors.

execCommand patibility

And by now, this is deprecated, and should not be used.

You can create functions to trigger undo and redo functionality on button click.

Undo button functionality as follows:

const handleUndoButtonClick = () => {
  document.execCommand('undo', false, '');
};

Redo button functionality as follows:

const handleRedoButtonClick = () => {
  document.execCommand('redo', false, '');
};

Call handleUndoButtonClick() on undo button click and call handleRedoButtonClick() on redo button click.

发布评论

评论列表(0)

  1. 暂无评论