【问题标题】:Quick way to add an or statement on a Jquery keydown function在 Jquery keydown 函数上添加 or 语句的快速方法
【发布时间】:2021-08-25 00:36:26
【问题描述】:

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

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


)}

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

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

)}

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

【问题讨论】:

  • $.ctrl() 不是核心 jQuery,可能是一个插件函数。需要链接到文档才能提供帮助。如果它是您代码库中的自定义函数,则需要查看它的代码

标签: javascript jquery dom keyboard-shortcuts keydown


【解决方案1】:

我认为您正在考虑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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 2013-05-15
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多