Updating keyboard focus when implementing the skip to main content technique through an anchor link

前端 未结 1 1287
遇见更好的自我
遇见更好的自我 2021-01-24 20:38

I\'m implementing the skip to main content technique described in W3C\'s WCAG2.0 Techniques documentation, however I\'m struggling to update the keyboard focus when act

相关标签:
1条回答
  • 2021-01-24 21:27

    From your comment, 'window.location.hash' is empty on click.

    I would also suggest that you apply tabindex="-1" rather than 0, which means the main is in the tab-order. Using -1 means you can programmatically focus on the element, but it is not in the default order.

    Also, for posterity (and people finding this later) my generally solution would be to use something like this:

    HTML Skip link:

    <a href="#main" id="skiplink">Skip to content</a>
    

    HTML Target:

    <main role="main" id="main" tabindex=”-1”>
    

    JS:

    $("#skiplink").click(function() {
        $('main').focus();
    });
    

    CSS:

    main:focus {outline: 0;}
    

    You can of course apply more of that through JavaScript, that's up to you.

    It might help to put the tabindex attribute in the HTML.

    0 讨论(0)
提交回复
热议问题