Ruby on Rails 键盘快捷键
有谁知道如何在我的网站上使用 Ruby on Rails 设置键盘快捷键?例如,如果用户想使用键盘快捷键与网站交互,而不是单击按钮/链接,我该怎么做?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有谁知道如何在我的网站上使用 Ruby on Rails 设置键盘快捷键?例如,如果用户想使用键盘快捷键与网站交互,而不是单击按钮/链接,我该怎么做?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
最简单的方法是为元素的
accesskey
属性设置一个值。如果您想通过 Rails 执行此操作,可以通过向submit_tag
辅助方法传递一个额外选项来实现此目的,如下所示:这将导致在 Alt< 时“单击”该按钮/kbd>+R(或 Alt+Shift+R,具体取决于您的浏览器)被按下。
accesskey
属性可用于、
如果您想要做一些更复杂的事情(例如 GMail 的键盘快捷键),您将必须编写一些 JavaScript。它的核心是一个事件处理程序,用于监视文档上的按键,然后在按下某个键时调用其他 JavaScript 函数来运行您想要的代码。这是一种基于按键设置快捷方式的非常简单的方法(这使用 Prototype,这是 Rails 默认使用的 Javascript 库,并且未经测试):
这是另一个 SO 问题,涉及 Javascript 中的键盘快捷键。
请注意,这些解决方案都根本不依赖于 Rails。
The simplest way is to set a value for the
accesskey
attribute for your elements. If you wanted to do this through Rails, you could do this by passing an extra option to thesubmit_tag
helper method, like so:Which will cause that button to be "clicked" when Alt+R (or Alt+Shift+R, depending on your browser) is pressed. The
accesskey
attribute is available for<input>
,<button>
and<a>
HTML elements.If you are looking to do something more complex (like GMail's keyboard shortcuts, for example), you will have to write some javascript. The core of it would be an event handler that watches for keypresses on the document, and then calls other javascript functions to run the code that you want when a certain key is pressed. Here's a very simplistic way of setting up shortcuts based on a key press (this uses Prototype, which is the Javascript library that Rails uses by default, and is untested):
Here is another SO question that deals with keyboard shortcuts in Javascript.
Note that neither of these solutions really rely on Rails at all.
查看此页面 关于 Web 应用程序的键盘快捷键。该站点提供了执行此功能的 .js。
Look on this page about keyboard shortcuts to a web application. This site supplies a .js that performs this functionality.
您可以使用任何 JavaScript 库来提供键盘快捷键。由于 Rails 默认使用 Prototype (对于 JavaScript 辅助方法),您可能需要查看 原型热键。
该网站中有一些使用示例。
You can use any JavaScript library to provide keyboard shortcuts. Since Rails uses Prototype by default (for the JavaScript helper methods), you might want to check out prototype-hotkeys.
There are some usage examples in that website.