【问题标题】:How can I make the 'aria-expanded' value back to 'false' when I move to the next element?当我移动到下一个元素时,如何使 \'aria-expanded\' 值恢复为 \'false\'?
【发布时间】:2023-01-16 21:55:18
【问题描述】:

我的菜单中有 2 个下拉按钮,我为其添加了 'aria-expanded'=false 默认值。当按钮展开或关闭时,还有添加到按钮的“向上”和“向下”类。当我单击第一个按钮将其展开时,“aria-expanded”值变为“true”并添加了“up”类。然后我点击第二个按钮,第二个按钮的 'aria-expanded' 值变为 'true' 并添加 class='up' 。但是这里开始了问题。即使当我单击第二个按钮时第一个按钮下拉菜单关闭,并且添加了“down”类,第一个按钮的“aria-expanded”值仍然为“true”。它不会变回“假”。 [按钮][1]

这是 jQuery 代码:

function initMenuDrowpDownHandlers() {
        $('.cfs_dropdown').parent().children('button').off('click.cfs_dropdown');
        $('.cfs_dropdown').parent().children('button').on('click.cfs_dropdown', function(e) {
            var lnk = $(this);
            console.log(lnk);
            if (lnk.hasClass('up')) {
                lnk.removeClass('up').addClass('down');
                lnk.attr("aria-expanded","false");
            } else {
                $('.cfs_dropdown').parent().children('button').removeClass('up').addClass('down');
                lnk.removeClass('down').addClass('up');
                lnk.attr("aria-expanded","true");
                $(document).on('click.cfs_drop', function (e) {
                    if ($(e.target).hasClass('disabled') || ($(e.target).parents('.cfs_dropdown')[0] && !$(e.target).is('button'))) return;
                    lnk.removeClass('up').addClass('down');
                    lnk.attr("aria-expanded","false");
                    $(document).off('click.cfs_drop');
                });
            }
            return false;
        });
    }

我将不胜感激任何建议。谢谢你。

【问题讨论】:

    标签: html jquery wai-aria


    【解决方案1】:

    尝试使用blur event

    yoursButton.addEventListener('blur', () => {
      yoursButton.setAttribute("aria-expanded", "false");
    });
    

    【讨论】:

    • 这是很好的第一步,但在下拉列表中,列表框中的选项也需要聚焦,这会使按钮模糊。在这种情况下,您不希望设置该属性。
    猜你喜欢
    • 1970-01-01
    • 2022-06-20
    • 2015-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    • 2020-07-05
    相关资源
    最近更新 更多