javascript - 在 Jquery keydown 函数上添加 or 语句的快速方法
问题描述
假设我有一个这样的 keydown 元素,
$.ctrl('13', function(){
)}
这将得到 ctrl + enter。但是,我还希望代码在 Mac 上的 command + enter 上运行。有没有一种方法,在函数上没有太多改变,在函数的开头有一个 or 语句,就像这样
$('.ctrl', '.metaKey')('13', function() {
)}
我想象这样的事情就像 ctrl 或 metakey + enter。有没有办法在 Jquery 中做到这一点?
解决方案
我认为您正在考虑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/
推荐阅读
- java - 为什么 List.contains(Object) 的行为不同?
- r - 如何在同一个数据帧上做colsum和average
- powerbi - 使用 DAX 生成日期系列
- javascript - 仅在服务器上需要一个包
- java - Intellij IDEA 仅针对所有未提交的更改运行测试
- regex - 在换行符之前匹配字符,不包括空格?
- macos - MacOS - 使用带有launchd的脚本 - 启动,登录,注销,关闭?
- service-worker - Workbox 的服务人员在更改时未更新
- python - AllenNLP 共指分辨率的多 GPU 训练
- reactjs - 事件处理程序中带有 [name] 的 PrevState