Listen to undo/redo event in contenteditable div

安稳与你 提交于 2020-02-21 13:04:49

问题


Is there a way to listen on all the ways a user could trigger an undo on a contenteditable div? For example when the user hits Control+Z, Right-click -> Undo, or in the file menu Edit -> Undo.

I'm not looking for undo/redo algorithms or implementations, just the ability to listen to the event and overwrite the behavior.


回答1:


Well, the Ctrl+Z/Y is possible, but I don't know about the Right-click->Undo/Redo part.

$(document).keydown(function (e) {
  var thisKey = e.which;

  if (e.ctrlKey) {
    if (thisKey == 90) alert('Undo');
    else if (thisKey == 89) alert('Redo');
  }
});



回答2:


Control z example at How to detect Ctrl+V, Ctrl+C using JavaScript?

for rightclick you can show a div on a right click How can I capture the right-click event in JavaScript?

then when they click the undo option on the div. just put in a onclick function.



来源:https://stackoverflow.com/questions/13319723/listen-to-undo-redo-event-in-contenteditable-div

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!