首页 > 解决方案 > 在 Jquery keydown 函数上添加 or 语句的快速方法

问题描述

假设我有一个这样的 keydown 元素,

$.ctrl('13', function(){


)}

这将得到 ctrl + enter。但是,我还希望代码在 Mac 上的 command + enter 上运行。有没有一种方法,在函数上没有太多改变,在函数的开头有一个 or 语句,就像这样

$('.ctrl', '.metaKey')('13', function() {

)}

我想象这样的事情就像 ctrl 或 metakey + enter。有没有办法在 Jquery 中做到这一点?

标签: javascriptjquerydomkeyboard-shortcutskeydown

解决方案


我认为您正在考虑event.ctrlKey与控制键结合使用的任何键。您可以像这样访问它:

$(document).on('keypress', function(e) {
  if (e.keyCode == 13 && e.ctrlKey)
    alert("Ctrl + enter was pressed!!");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p>Click in this area to focus in this $(document) and press Ctrl+Enter
  <?p>

https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/ctrlKey https://api.jquery.com/category/events/event-object/


推荐阅读