简体   繁体   English

<a>带孩子的</a> HTML tabindex

[英]HTML tabindex on <a> with children

I have a toggling navigation menu structured the following way: 我有一个按以下方式构造的切换导航菜单:

<li>
  <a href="mylink.page">
    <span class="text">Link Text</span>
    <span tabindex="0" class="button"></span>
  </a>
  <ul>... children ... </ul>
</li>

span.text houses the link text and span.button is the element that a user can click or keypress enter to toggle the nested ul. span.text包含链接文本,而span.button是用户可以单击或按Enter切换嵌套ul的元素。

While tabbing through the menu (testing accessibility), my browser (tried in both latest public versions of Chrome and Firefox on Windows) focuses on the container a, then focuses on span.text, then finally on span.button. 在浏览菜单时(测试可访问性),我的浏览器(在Windows的最新公共版本的Chrome和Firefox中都尝试过)着重于容器a,然后着重span.text,最后着重span.button。 This is the behavior I want but don't want the container 'a' to get focused on - just go straight to span.text. 这是我想要的行为,但不想让容器“ a”集中精力-直接进入span.text。

Note: When focused on container 'a', hitting enter does not activate the link, only the children spans activate the link when focused upon (I have prevented the link from working when keypress enter on span.button so the toggling works on keypress enter). 注意:当专注于容器“ a”时,按Enter不会激活该链接,只有子跨度会在关注时激活该链接(我阻止了该链接在span上按Enter时该链接起作用。 )。

This behavior is fine and is essentially how I want it (spans being the links activated by keypress enter) expect I want to skip the container 'a' when tabbing and go straight to the children spans. 此行为很好,本质上是我想要的方式(跨度是通过keyEnter输入激活的链接),期望我想在制表时跳过容器“ a”并直接转到子跨度。

tabindex="-1" right? tabindex =“-1”对吗? Nope. 不。 When adding tabindex="-1" to the container 'a', span.text no longer gets focused and an active link is gone. 当将tabindex =“-1”添加到容器'a'时,span.text不再集中并且活动链接消失了。 The browser focuses on the container 'a' and then skips over span.text to span.button. 浏览器将焦点放在容器“ a”上,然后跳过span.text到span.button。 This is odd (at least to me). 这很奇怪(至少对我来说)。 In my mind, tabindex="-1" is used for skipping the element while tabbing. 在我看来,tabindex =“-1”用于在制表时跳过元素。

Any ideas how I can use tabindex on a combination of the container 'a' and the children spans to achieve what I stated above (tabbing through menu only hits span.name [which has active link] and span.button)? 有什么想法可以在容器“ a”和子级span的组合上使用tabindex来实现我上面提到的内容(通过菜单进行tab只能命中span.name [具有活动链接]和span.button)? Or even a javascript/jquery solution? 甚至是javascript / jquery解决方案?

Note: Having tabindex="0" seems to be the only way to tab to span.button. 注意:tabindex =“ 0”似乎是制表符到span.button的唯一方法。

EDIT: After tinkering for a bit, I put tabindex="0" on the container 'a' and it works beautifully in Chrome. 编辑:修补了片刻之后,我将tabindex =“ 0”放到了容器'a'上,它在Chrome浏览器中表现出色。 Firefox still focuses on the container 'a'. Firefox仍然专注于容器“ a”。

In jQuery, try: 在jQuery中,尝试:

$('a:has(span[tabindex])').on('focus', function () {
    $(this).find('span[tabindex]').focus();
}).on('keypress', 'span[tabindex]', function (e) {
    if(e.which === 13) { 
       this.click(); //this not $(this), to call native click method
       //or: $(this).parent()[0].click();
    }
});

For jQuery below 1.7, try: 对于1.7以下的jQuery,请尝试:

$('a:has(span[tabindex])').bind('focus', function () {
    $(this).find('span[tabindex]')[0].focus(); // call js focus(), not jq
}).delegate('span[tabindex]', 'keypress', function (e) {
    if (e.which === 13) {
        this.click(); //this not $(this), to call native click method
        //or: $(this).parent()[0].click();
    }
});

To ensure you get to the child spans first, you may want to start assigning them lower numbers which will be addressed by the browser first. 为确保您首先到达子范围,您可能要开始为其分配较小的数字,然后浏览器将首先解决这些数字。 Based on the W3 spec it appears you could go to the trouble of assigning parent elements very high (up to 32767) values to avoid dealing with them. 根据W3规范 ,您似乎可能会麻烦地分配非常高(最多32767)的父元素以避免处理它们。 If the user agent is following the spec, it should address the lowest values first and work it's way up. 如果用户代理遵循规范,则应首先解决最低值,然后逐步提高。

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

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