简体   繁体   English

如何在输入字段以外的元素上触发聚焦?

[英]How to trigger focusout on an element other than an input field?

I'm trying to trigger a focusout event on an ul element: 我试图在ul元素上触发focusout事件:

// HTML
<nav>
  <ul _v-2e9e2f12="">
    <li _v-2e9e2f12="">
      <a _v-2e9e2f12="">
      </a>
      <ul _v-0078ee36="" _v-2e9e2f12="">
        <li>List element</li>
        <li>List element</li>
        <li>List element</li>
      </ul>
    </li>
  </ul>
</nav>

// JS
$(document).ready(function() {
  $("a").click(function(ev) {
    var $icon = $(ev.currentTarget)
    var $menu = $icon.next()
    $menu.focus()
    console.log('Focus triggered!')
  });

  $("ul[_v-0078ee36]").focusout(function() {
    console.log('Focusout triggered!')
  });
})

But .focusout is never triggered. 但是.focusout永远不会触发。

What's the correct way of doing it? 正确的做法是什么?

Here's the JSFiddle . 这是JSFiddle

Elements other than input elements cannot be focused unless you give a tab index to them. 除非您为它们指定选项卡索引,否则除输入元素外的其他元素都无法聚焦。

<ul tabindex="-1" _v-0078ee36="" _v-2e9e2f12="">

So if you want to trigger the focus out for a non input element, you have to set its tab index to -1 . 因此,如果要触发非输入元素的焦点,则必须将其选项卡索引设置为-1

DEMO DEMO

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

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