通过jquery调用Escape键按下事件 [英] Invoke Escape key press event by jquery
问题描述
我想澄清我的问题.请不要将其与确定按键按键混淆. 我不希望用户按任何键.相反,我想运行一个脚本,该脚本触发Escape键按下事件. 请帮忙!
I want to make my question clear. Please dont confuse it with determining the key press. I dont want the user to press any key. On the contrary, I want to run a script which fires the Escape key press event. Please help!
推荐答案
您可以使用$.Event()
创建一个Event对象,该事件对象包含keyCode为27(应为Esc)的keydown事件,例如:
You can use $.Event()
to create an Event object containing a keydown event with the keyCode of 27 (which should be Esc) like:
var esc = $.Event("keydown", { keyCode: 27 });
$("body").trigger(esc); // change body to the element where you'd like to execute the escape key press.
请参见 此工作示例 和 jquery.com上的文档
See this working demo and the docs at jquery.com
还请注意,正如Christofer所说,这可能并非在所有浏览器和环境中都有效,因为它可能被认为是可疑可能被恶意使用的行为.方式-就像使用外部href 不会在<a>
上触发点击事件一样 .我在Chrome,Safari,Opera,FF& amp; IE9.
Also note that, as stated by Christofer, this might not be working in all browsers and environments as it might be considered shady behavior that could be used in a malicious manner - just like triggering a click event on an <a>
with an external href will not happen. I successfully tested this (the Esc) in Chrome, Safari, Opera, FF & IE9 though.
这篇关于通过jquery调用Escape键按下事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!