javascript 通过javascript触发向上或向下箭头键事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28347883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trigger an UP or DOWN ARROW key event through javascript
提问by Rishab Gupta
There is some event listener in Thunderbird which resolves the emails address from my LDAP server when i press the UP or DOWN ARROW key. so i want to trigger that the same event through Javascript without having to physically press the up or down arrow key. How can i do this?
当我按向上或向下箭头键时,Thunderbird 中有一些事件侦听器可以解析来自我的 LDAP 服务器的电子邮件地址。所以我想通过 Javascript 触发相同的事件,而不必物理按下向上或向下箭头键。我怎样才能做到这一点?
采纳答案by a_pradhan
I believe this is what you want (using JQuery)
我相信这就是你想要的(使用 JQuery)
var e = jQuery.Event("keyup");
// e.which is used to set the keycode
e.which = 38; // it is up
e.which = 40; // it is down
$("id_to_element").trigger(e);
If JQuery is not allowed to use, pure javascript solution is more verbose. See this answer
如果不允许使用 JQuery,纯 javascript 解决方案更加冗长。看到这个答案
Note: There maybe a bug in Chrome which will hamper this. I would recommend JQuery for less headache.
注意:Chrome 中可能有一个错误会阻碍这一点。我会推荐 JQuery 来减少头痛。