首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery事件触发器在注释和seadragon上不起作用

jQuery事件触发器在注释和seadragon上不起作用
EN

Stack Overflow用户
提问于 2021-06-08 03:44:28
回答 3查看 48关注 0票数 0

我正在尝试使用jQuery自动触发向下箭头键事件。注解/seadragon组合有一个侦听器,当我按下向下箭头时,它会打开所有预配置的标记。

我已经编写了jQuery代码来查找输入字段,将焦点放在该字段上,然后触发keyup事件。

代码语言:javascript
复制
    function triggerDownArrowOnInput() {
        $("[id^=downshift][id$=input]").each(function(index) {
            // There should only be 1, but let's not assume.
            console.log(index);
            if (index == 0) {
                console.log("Found an input: " + $(this).attr("id"))
                $(this).focus();
                var event = jQuery.Event("keyup");
                event.keyCode = event.which = 40; // down arrow
                $(this).trigger(event);
            } else {
                console.log("Multiple elements found that match the id: " + $(this).attr("id"));
            } // if
        })
    } // triggerDownArrowOnInput

焦点工作得很好,但不是导火索。如果我手动点击向下箭头键,则所有预配置的标记都会出现:

我已经分别试过"keyCode“和"which”了。

我已经尝试触发$(This).keyup(事件)。

我已经尝试在焦点调用和触发器/按键调用之间设置一个延迟。

我尝试调用$(Document)事件(.trigger)。

我认为我可能将事件发送到了错误的元素,但似乎(通过Dev工具)只有输入字段和文档启用了侦听器。

无论我做什么,我都不能让事件触发。有什么想法吗?

谢谢。

EN

回答 3

Stack Overflow用户

发布于 2021-06-09 05:33:30

我想我可以在没有jQuery的情况下,使用KeyboardEventdispatchEvent来完成这个任务。在我的测试中,我认为你不需要事先获得焦点,因为它是元素上的一个事件,但值得在你的应用程序上进行测试。

代码语言:javascript
复制
function triggerDownArrowOnInput() {
    $("[id^=downshift][id$=input]").each(function(index) {
        // There should only be 1, but let's not assume.
        console.log(index);
        if (index == 0) {
            console.log("Found an input: " + $(this).attr("id"))
            $(this).focus();
            this.dispatchEvent(new KeyboardEvent('keyup',{'keyCode': 40, 'key':'ArrowDown', 'code':'ArrowDown'}));
        } else {
            console.log("Multiple elements found that match the id: " + $(this).attr("id"));
        }
    })
}
票数 1
EN

Stack Overflow用户

发布于 2021-06-08 05:17:03

你试过keydown吗?

代码语言:javascript
复制
var e = jQuery.Event("keydown");
e.which = 40;
e.keyCode = 40
$(this).trigger(e);

代码语言:javascript
复制
function triggerDownArrowOnInput() {
    $("[id^=downshift][id$=input]").each(function(index) {
        // There should only be 1, but let's not assume.
        console.log(index);
        if (index == 0) {
            console.log("Found an input: " + $(this).attr("id"))
            $(this).focus();
            var event = jQuery.Event("keydown");
            event.keyCode = event.which = 40;
            $(this).trigger(event);
        } else {
            console.log("Multiple elements found that match the id: " + $(this).attr("id"));
        }
    })
} // triggerDownArrowOnInput

票数 0
EN

Stack Overflow用户

发布于 2021-06-10 02:58:20

我能够让事件触发,但仍然无法打开focus菜单。我最终不得不为以下内容创建一个开发环境:

代码语言:javascript
复制
recogito/recogito-client-core
recogito/recogito-js
recogito/annotorious
recogito/annotorious-openseadragon

然后,我修改了recogito/recogito-client- Autocomplete.jsx中的核心,添加了一个OnFocus侦听器,然后添加了以下代码:

代码语言:javascript
复制
const onFocus = evt => {
    if (!isOpen) {
        this.setState({ inputItems: this.props.vocabulary }); // Show all options on focus
        openMenu()
    } // if
} // onFocus

比我想做的要多得多,但它现在起作用了。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67877815

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档