简体   繁体   English

如何使Javascript / jQuery键事件与Screen Readers一起使用

[英]How to make Javascript/jQuery key events work with Screen Readers

I have the following jsFiddle containing a navigation bar consisting of a few categories with sub categories in them. 我有以下jsFiddle,其中包含一个导航栏,其中包含一些类别,其中包含子类别。

<div class="navigation-bar">
    <div class="category-container" tabindex="0" role="navigation" aria-labelledby="category-1">
        <h4 id="category-1">Category</h4>
        <ul class="subcategory">
            <li tabindex="1"><a href="#">Subcategory</a></li>
            <li tabindex="2"><a href="#">Subcategory</a></li>
            <li tabindex="3"><a href="#">Subcategory</a></li>
            <li tabindex="4"><a href="#">Subcategory</a></li>
        </ul>
    </div>
    <div class="category-container" tabindex="0" role="navigation" aria-labelledby="category-2">
        <h4 id="category-2">Category</h4>
        <ul class="subcategory">
            <li tabindex="1"><a href="#">Subcategory</a></li>
            <li tabindex="2"><a href="#">Subcategory</a></li>
            <li tabindex="3"><a href="#">Subcategory</a></li>
            <li tabindex="4"><a href="#">Subcategory</a></li>
        </ul>
    </div>
</div>

I am leveraging Javascript/jQuery like so: 我像这样利用Javascript / jQuery:

$(function(){
    $(".category-container").keydown(function(e){
        // down arrow shows the category sub menu, if said sub menu is hidden
        if($(this).find(".subcategory").is(":hidden") && e.keyCode === 40){
            $(this).find(".subcategory").toggle();
        }
        // esc hides the category sub menu, if said sub menu is visible
        else if($(this).find(".subcategory").is(":visible") && e.keyCode === 27){
            $(this).find(".subcategory").toggle();
        }
    });

    $("a").keydown(function(e){
        // if we press tab on the last sub category, hide current category sub menu
        if($(this).closest(".subcategory").find("li:last-child a").is(":focus") && e.keyCode === 9){
            $(this).closest(".subcategory").toggle();
        }
    });
});

As you can see, the navigation works very well for people without disabilities, meaning you can use your mouse to explore the navigation bar using simple CSS event handlers. 如您所见,该导航功能对残障人士非常有效,这意味着您可以使用鼠标使用简单的CSS事件处理程序来浏览导航栏。

For keyboard accessibility, I have these very specific instructions: 对于键盘的可访问性,我有以下非常具体的说明:

  • Navigate from category to category via tab press. 通过tab键在类别之间导航。
  • To open a sub categories menu, press the down arrow key. 要打开子类别菜单,请按down arrow键。
  • To close a sub categories menu, press the esc key. 要关闭子类别菜单,请按esc键。
  • To navigate from sub menu item to sub menu item, press tab . 要从子菜单项导航到子菜单项,请按tab

All of that functionality is already in place thanks to Javascript/jQuery. 由于有了Javascript / jQuery,所有这些功能已经就位。 My challenge comes when using a screen reader, like JAWS or NVDA, which is currently preventing my Javascript/JQuery from working as above described for keyboard navigation purposes. 当使用屏幕阅读器(如JAWS或NVDA)时, 我面临的挑战是当前如何阻止我的Javascript / JQuery出于键盘导航目的而如上所述工作。

To be specific, I cannot access the subcategories via any combination of keypress events I could come up with. 具体来说,我无法通过我可能遇到的keypress事件的任何组合来访问subcategories

Question: Is it possible to fix this code to be keyboard accessible and ARIA compliant? 问题:是否可以将此代码修复为可通过键盘访问并且符合ARIA? Even if it is not possible, an explanation towards why it would not be possible to fix this code is better than nothing. 即使不可能,对为什么无法修复此代码的解释总比没有好。

Your code lacks of some aria attributes to indicate that your menu is a dropdown menu and explanations to describe which shortcut you might use. 您的代码缺少一些aria属性,以表明您的菜单是一个下拉菜单,并缺少说明来描述您可能使用的快捷方式。 Down arrow and esc key are not commonly used in web navigation. Down arrowesc键在网络导航中不常用。

Something like aria-haspopup for instance. 例如aria-haspopup类的东西。

You can see some examples with a simple google search "aria dropdown menu", like: 您可以通过简单的Google搜索“ aria下拉菜单”查看一些示例,例如:

I would say that the most difficult part is to have some shortcuts which will work intuitively. 我要说的是,最困难的部分是拥有一些可以直观工作的快捷方式。

The first example is something I like because it can be used with the tab key and add a pragmatic left/right (but optional) arrow shortcut to go to the next category. 第一个示例是我喜欢的东西,因为它可以与tab键一起使用,并添加实用的向左(向右)(但可选)的箭头快捷方式以转到下一个类别。

Concerning your code, I think that the down arrow might override the navigation key of your screenreader, so using enter key will be something which should work in your case. 关于您的代码,我认为向下箭头可能会覆盖屏幕阅读器的导航键,因此使用enter键将在您的情况下起作用。

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

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