简体   繁体   English

jQuery:使用标签在菜单中导航

[英]Jquery: Navigating in menu using tab

I want to find a general rule that will help me navigate in menus, using tab. 我想找到一条通用规则,可以帮助我使用Tab键在菜单中导航。 I know how to catch the tab event but there are several problems: 我知道如何捕获选项卡事件,但是有几个问题:

  1. There are drop-down menus that i can't accese only be pressing tab key. 我仅通过按Tab键就无法访问下拉菜单。
  2. The structure of the drop-down menus is not standard. 下拉菜单的结构不是标准的。 ie the hidden ul elements may be inside other elements (eg div) or may be nested drop-down menus inside other drop-down menus 即,隐藏的ul元素可能位于其他元素(例如div)内,或者可能是嵌套在其他下拉菜单内的下拉菜单
  3. The events that make the hidden menus to be visible/invisible vary. 使隐藏菜单可见/不可见的事件有所不同。 Click and hover events are the most popular. 单击和悬停事件是最受欢迎的。

Can someone help me? 有人能帮我吗?

You can use the tabindex attribute on the list item that triggers the showing of your dropdown. 您可以在触发下拉列表显示的列表项上使用tabindex属性。 This wil make the parent li focusable by pressing tab. 通过按Tab键可以使父级li变得可聚焦。 You can then, with some CSS and Javascript, apply rules to children to make them visible. 然后,您可以使用一些CSS和Javascript将规则应用于子项以使其可见。

About the fiddle: 关于小提琴:

Note that I use the opacity and pointer-events properties in CSS to switch states of the dropdown. 请注意,我使用CSS中的opacitypointer-events属性来切换下拉菜单的状态。 When an element has visibility: hidden or display: none applied, it will not be focusable at all. 当元素具有visibility: hiddendisplay: none应用时,将根本无法聚焦。 Since the tab key by default brings focus to the next focusable element, your target element should not have either of those properties set to the shown values at the point the tab key is being pressed. 由于默认情况下,Tab键将焦点移至下一个可聚焦元素,因此在按Tab键时,目标元素不应将这些属性中的任何一个设置为显示的值。

Also, you'll have to accurately keep track of the tabindexes throughout your navigation. 另外,您还必须在整个导航过程中准确跟踪tabindex。 First is the first main anchor, then the enclosing list-item, to make the dropdown visible. 首先是第一个主锚,然后是封闭的列表项,以使下拉列表可见。 Then come the anchors inside that. 然后进入里面的锚点。 (this is the point where JS has to take over from CSS) This brings us at tabindex 5 when the 3 subItems inside are assigned a tabindex, so we'll continue counting from 6 for the next main item's direct <a> child, and so forth. (这是JS必须从CSS接管的地方),这给我们带来了tabindex 5的好处,这时为内部的3个子项分配了tabindex,因此我们将从6开始继续为下一个主要项目的直接<a>子项计数,并且等等。

You'll have to figure out the way to make your multi-level dropdowns work as expected yourself, but this is a starting point. 您必须找出使自己的多级下拉菜单正常工作的方法,但这是一个起点。 You could additionally check in your script for arrow key presses, and give another element focus in response. 您还可以在脚本中签入箭头键,并以其他元素作为响应。

Fiddle: 小提琴:

 $(function() { $('nav').on('focus', '.dropdown a', function() { $(this).closest('.mainItem').addClass('focus'); }).on('blur', '.dropdown a', function() { $(this).closest('.mainItem').removeClass('focus'); }); }); 
 nav > ul { display: flex; } nav ul { list-style: none; margin: 0; padding: 0; } nav .mainItem { position: relative; margin: 0 5px; padding: 0.5rem; background-color: #eee; } nav .dropdown { position: absolute; width: 100%; left: 0; top: 100%; opacity: 0; pointer-events: none; } nav .mainItem:hover .dropdown, nav .mainItem:focus .dropdown, nav .mainItem.focus .dropdown, nav .mainItem > a:hover .dropdown, nav .mainItem > a:focus .dropdown nav .mainItem.focus .dropdown { opacity: 1; pointer-events: all; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav> <ul> <li class="mainItem" tabindex="2"><a href="#" tabindex="1">Parent 1</a> <ul class="dropdown"> <li><a href="#" tabindex="3">Child 1</a></li> <li><a href="#" tabindex="4">Child 2</a></li> <li><a href="#" tabindex="5">Child 3</a></li> </ul> </li> <li class="mainItem" tabindex="7"><a href="#" tabindex="6">Parent 2</a> <ul class="dropdown"> <li><a href="#" tabindex="8">Child 1</a></li> <li><a href="#" tabindex="9">Child 2</a></li> <li><a href="#" tabindex="10">Child 3</a></li> </ul> </li> </ul> </nav> 

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

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