如何在js中触发向下箭头键? [英] How to trigger arrow down press in js?

查看:73
本文介绍了如何在js中触发向下箭头键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以填充并聚焦,但是我需要添加一个向下箭头事件,因为它将触发列表弹出,如下图所示.有人可以帮我吗?

I have this code that fills and focuses but I need to add an arrow down event as it will trigger list popup like pictured below. Can anyone help me with the line?

document.body.appendChild(element)

    element.addEventListener('click', function(){
        document.querySelector("input#generic_test_order_search.ui-autocomplete-input").value = '16048'
        document.querySelector("input#generic_test_order_search.ui-autocomplete-input").focus()

     })


})();

推荐答案

信息不足.最好提供完整的HTML ...

Not enough information. It might be better to provide full HTML...

(有点关联的问题:脚本在网页输入框中自动填充值)

1-此处的目标是获取建议下拉菜单的 class .然后在里面找到 children ,也许在里面找到另一个 children ,然后在其上触发 click().

1 - The goal here is to get class of the suggestion dropmenu. And find children inside, and maybe another children inside, and then trigger click() on it.

由于很难猜测类的名称和结构,因此这里是等价示例,有关如何单击下拉建议.

Since it is pretty hard to guess the class names and the structure, here is a pretty much equal example of how to click on drop-down suggestion.

a)这是维基百科.打开链接. https://en.wikipedia.org/wiki/Stack_Exchange

a) Here is Wikipedia. Open the link. https://en.wikipedia.org/wiki/Stack_Exchange

b)将以下代码另存为书签:

b) Save the following code as a bookmarklet:

javascript:(function(){
    document.getElementsByClassName('suggestions')[0].children[0].children[1].click();
})();

c)然后在Wikipedia中写三个字母 goo 进行搜索.

c) Then write three letters goo to the search in Wikipedia.

d)最后触发小书签.然后它将打开第二个建议的链接: children [1] .这就是它可能的工作方式.您可以使用自己的HTML尝试这样做.

d) And finally trigger the bookmarklet. Then it will open up the second suggested link: children[1]. This is how it may work. You might try like this with your HTML on your own.

2-制作 bookmarklet 可能比追加事件侦听器稍微好一点,因为您所需要做的只是插入值.

2 - There is a chance that making a bookmarklet might be a slightly better than appending an event listener, since all you need is to insert values.

另一个选项是 Tampermonkey/Greasemonkey ,它可以更自动地触发操作(实际上,此选项与小书签几乎相同,并且代码结构完全相同且完全兼容).

Another option is Tampermonkey / Greasemonkey to trigger things even more automatically (in fact, this option is pretty much the same as a bookmarklet, and the code structure is absolutely the same and fully compatible).

这篇关于如何在js中触发向下箭头键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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