如何在不知道哪个按钮具有焦点的情况下使用Enter键触发特定按钮. [英] How to fire a specific button with the Enter key without knowing which button has focus.

查看:142
本文介绍了如何在不知道哪个按钮具有焦点的情况下使用Enter键触发特定按钮.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是复杂的jquery和javascript的新手.我正在对具有多个面板和用户控件的aspx页面进行维护.每个面板都有一个或多个按钮,其规格为Enter键将成为默认键,无论哪个按钮具有焦点以符合508c法规要求.我试过了:为每个面板设置默认键,然后将div与div一起使用.到目前为止,研究已将带有事件的单个按钮ID发送到jquery或javascript.这些问题是每一个都会触发一个已知的按钮ID.或无论哪个按钮都具有焦点,都会执行默认的按键事件.脚本需要做的是:按下Enter键时,无论焦点是哪个按钮,都将触发具有焦点的按钮的按钮事件,而无需使用鼠标执行"click"事件.我意识到解析document.elements是一个选项,但是响应时间很长.我看到有人建议使用document.ActiveElement,但是我无法在维护的编码级别访问此属性.谢谢您能提供的任何帮助.

I am new to complex jquery and javascript. I am doing maintenance on aspx pages with multiple panels and user controls. Each panel has one or more buttons with the specification that the Enter key will be the default key no matter which button has focus to meet 508c Compliance. I have tried: setting the default key for each panel, and using keydown with a div. The research so far sends a single button id with the event to a jquery or javascript. The problem with these is each fires a already known button id. OR performs the default key event no matter which button has focus. What the script needs to do is: When the Enter Key is hit, the button event for the button that has focus will fire without using the mouse to perform the "click" event, no matter which button it is. I realize that parsing the document.elements is an option, however very response time consuming. I see document.ActiveElement is being suggested, however I do not have access to this property at the coding level that I am maintenancing. Thank you for any help I can get.

推荐答案

模拟按钮单击和在UI开发中使用已知的反专利技术是一个坏主意. />
如果考虑目的,您将看到正确的方法.您实际上并不需要触发特定按钮".您真正需要做的是,它调用与用户单击按钮时调用的功能相同的功能.

只需采用它并完全按照这种方式即可.定义一些函数,然后从代码的两个(或更多)不同位置调用它.一个是来自事件处理程序的某个按钮单击,另一个是来自其他位置的单击.只有这样,您才能在调用某些操作时实现合理的UI一致性.



此处介绍了获取焦点元素的方法之一:
http://stackoverflow.com/questions/7329141/how-to- get-current-focused-element-in-javascript [ ^ ].

另外,请注意jQuery:它可以为您提供很多帮助:
http://en.wikipedia.org/wiki/Jquery [ http://jquery.com/ [ ^ ].

—SA
It''s a bad idea to simulate a button click, and a known anti-patten in UI development.

You will see the right approach if you consider the purpose of it. You don''t really need "to fire a specific button". What you really need to do, it to call same function as the one called when the user clicks a button.

Just take it and make it — exactly in this way. Define some function and call it from two (or more) different places of the code. One would be from the event handler of some button click, another one — elsewhere. Only in this way you will achieve reasonable UI consistency in invocation of some actions.



One of the ways to get a focused element is described here:
http://stackoverflow.com/questions/7329141/how-to-get-current-focused-element-in-javascript[^].

Also, pay attention for jQuery: it can help you a lot:
http://en.wikipedia.org/wiki/Jquery[^],
http://jquery.com/[^].

—SA


这篇关于如何在不知道哪个按钮具有焦点的情况下使用Enter键触发特定按钮.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆