问题
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