简体   繁体   English

按键事件如何与不会引起任何关注的元素一起使用

[英]How does keypress events works with element which doesn't take any focus

I need to attach a key press event on a 我需要在键盘上附加一个按键事件

  • element. 元件。 I know we can use $(":li").live() will do that but since li is not a focusable element how can it take the key press event. 我知道我们可以使用$(“:li”)。live()来做到这一点,但是由于li不是可聚焦元素,因此如何处理按键事件。 Or when does the key press event gets fired for this elements? 或何时触发此元素的按键事件?

    Please help? 请帮忙?

  • An unfocusable element will not fire key events (although key events may bubble up from focusable elements contained within it). 无法聚焦的元素将不会触发关键事件(尽管关键事件可能会从其中包含的可聚焦元素中冒出来)。

    If you want to make the element focusable and receive key events, you could set its tabindex , either as an attribute in HTML or via JavaScript. 如果要使元素可聚焦并接收键事件,可以将其tabindex设置为HTML中的属性或通过JavaScript设置。 Setting a tabindex value of 0 puts the element in the default tab order, which is the order elements appear in the DOM. 将tabindex值设置为0会将元素置于默认的标签顺序中,这是元素在DOM中出现的顺序。

    <li tabindex="0">...</li>
    

    To set all <li> elements as focusable in jQuery: 要将所有<li>元素设置为可在jQuery中聚焦:

    $("li").prop("tabIndex", 0);
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM