将Ctrl + Z分配给退出键

时间:2019-03-14 15:34:56

标签: jquery

我有一个功能,我想在按键Escape(27)上撤消上一个事件。 如果我按ctrl + z可以正常工作,但是我想将控件Z设置为Escape键

$('.toAct').not('.id').on('keyup', function(e) {
 if (e.which === 27) {
  e.preventDefault()

   // CTRL Z FUNCTION
   //$(this).click('ctrl+z')

  } 
})

1 个答案:

答案 0 :(得分:0)

您可以使用Document#executeCommand执行undo操作。

$('.toAct').not('.id').on('keyup', function(e) {
  if (e.which === 27) {
    e.preventDefault()
    document.execCommand('undo', false, null);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<textarea class="toAct"></textarea>

MDN docs for available commands.