输入键和标签不适用于焦点类

发布于 2025-01-21 19:06:30 字数 1782 浏览 2 评论 0原文

我试图在标题中出于可访问性的原因来标记按钮,但是我无法对其进行标签。

我有以下CSS

.nmcnav_clickable:focus {
    background-color: yellow;
  }

.nmcnav_clickable {
    .noButton;
    display: flex;
    align-items: center;
    height: var(--primary-height);
    cursor: pointer;
    // don't show an outline when not tabbing
    &:focus:not(:focus-visible) {
        outline: none;
    }
    &:focus {
        outline: currentColor solid;
        background-color: yellow;
    }

    &:hover {
        .nmcnav_text {
            color: @color-teal;
        }
    }
}

这是我的html,

    {% macro navbutton(item) %}
        <button tabindex="0"
            class="nmcnav_clickable nmcnav_button"
            aria-controls="nmcnav_dropdown-{{ item.id }}"
            aria-expanded="false"
            id="nmcnav_button-{{ item.id }}">
            <span class="nmcnav_text">{{ item.title|raw }}</span>
        </button>
    {% endmacro %}

    {% macro navlink(item) %}
    <a 
        class="nmcnav_clickable {{ item.meta('button') ? 'nmcnav_btn' : '' }}"
        href="{{ item.get_link }}"{% if item.target %} target="{{ item.target }}"{% endif %}>
        <span class="nmcnav_text {{ item.meta('button') ? 'btn' : '' }}">{{ item.title|raw }}</span>
    </a>
    {% endmacro %}

我可以通过按钮 tab> tab> tab> ,但后来我意识到我无法按下 enter 实际访问链接是因为Tabindex仅适用于&lt; a&gt;&lt; button&gt;标签。但是,当我尝试将Tabindex更改为按钮时,什么也不会发生。我需要能够标记并输入按钮。我还附上了标头的外观。

预先感谢。

I am trying to tab the buttons for accessibility reasons in the header however I am not able to tab over them.

I have the following css

.nmcnav_clickable:focus {
    background-color: yellow;
  }

.nmcnav_clickable {
    .noButton;
    display: flex;
    align-items: center;
    height: var(--primary-height);
    cursor: pointer;
    // don't show an outline when not tabbing
    &:focus:not(:focus-visible) {
        outline: none;
    }
    &:focus {
        outline: currentColor solid;
        background-color: yellow;
    }

    &:hover {
        .nmcnav_text {
            color: @color-teal;
        }
    }
}

This is my HTML

    {% macro navbutton(item) %}
        <button tabindex="0"
            class="nmcnav_clickable nmcnav_button"
            aria-controls="nmcnav_dropdown-{{ item.id }}"
            aria-expanded="false"
            id="nmcnav_button-{{ item.id }}">
            <span class="nmcnav_text">{{ item.title|raw }}</span>
        </button>
    {% endmacro %}

    {% macro navlink(item) %}
    <a 
        class="nmcnav_clickable {{ item.meta('button') ? 'nmcnav_btn' : '' }}"
        href="{{ item.get_link }}"{% if item.target %} target="{{ item.target }}"{% endif %}>
        <span class="nmcnav_text {{ item.meta('button') ? 'btn' : '' }}">{{ item.title|raw }}</span>
    </a>
    {% endmacro %}

There was a point where I was able to tab over the buttons but then I realized I was not able to press enter to actually access the links because tabindex only works with <a> or <button> tags. But when I try and change the tabindex to the buttons, nothing happens. I need to be able to tab over and enter for the buttons. I have also attached a ss of what the header should look like. enter image description here

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文