简体   繁体   English

如何使用键盘快捷键调用JavaScript函数?

[英]How to call a JavaScript function using keyboard shortcut?

There is a website which consists of main frame and some additional ones (they're responsible for parsing inserted data). 有一个网站,包含主要框架和一些其他框架(它们负责解析插入的数据)。 When I start to edit a record, new frame appears. 当我开始编辑记录时,出现新的框架。 In the source code I see some JavaScript scripts added. 在源代码中,我看到添加了一些JavaScript脚本。 Some of them come from site directories, but also there are some defined in the <head> section of the frame. 其中一些来自站点目录,但在框架的<head>部分中也有一些定义。 And there is a function defined, let's call it parseData(input){...} , which is then called upon pressing "Save" link. 并且定义了一个函数,我们称它为parseData(input){...} ,然后在按下“保存”链接时调用它。

Is there any way to assign a keyboard shortcut to execute saving? 有什么方法可以分配键盘快捷键来执行保存? There are no ids defined for the "Save" link, only href and class . 没有为“保存”链接定义任何ID,只有hrefclass

I tried bookmarklets*, but was stuck on joining the frame URL and the JS function call. 我尝试了书签*,但是被卡在了框架URL和JS函数调用中。 Any other ideas? 还有其他想法吗?

Edit: * bookmarklets are meant to be launched using Shortcut Manager-like web browser extension. 编辑:*小书签旨在使用类似快捷方式管理器的Web浏览器扩展启动。

Edit2: I'm not developing the website. Edit2:我不开发网站。 I have read-only access to it. 我具有只读访问权限。

If you want to use pure javascript to bind, say, "enter" to the function parseData, you could use: 如果要使用纯JavaScript进行绑定(例如“输入”)到parseData函数,则可以使用:

document.onkeydown = function(e){
    e = e || window.event;
    keycode = e.which || e.keyCode;
    if(keycode == 13){      // '13' is the keycode for "enter"
        e.preventDefault();
        parseData(input);
     }
}

You might want to use jQuery with this plugin to create keyboard shortcuts. 您可能想将jQuery与该插件一起使用来创建键盘快捷键。 Have a look at this: 看看这个:

https://github.com/jeresig/jquery.hotkeys https://github.com/jeresig/jquery.hotkeys

You may do something like this: 您可以执行以下操作:

$(document).on('keydown', null, 'ctrl+s', saveFunction);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM