【问题标题】:Force Screen Reader to Read When A Component Is Removed移除组件时强制屏幕阅读器阅读
【发布时间】:2021-10-07 00:23:35
【问题描述】:

删除所有过滤器项后,我需要屏幕阅读器阅读“所有过滤器均已删除”。棘手的部分是:当有源过滤器组件被移除时,我没有其他元素可以将aria-label 设置为All filters are removed。我想知道在这种情况下如何强制屏幕阅读器阅读内容?

HTML 示例在这里:

<div *ngIf="activeFilters.length > 0">
    <div class="active-filters" attr.aria-label="{{'List of active filters'}}">
        <h4 class="active-filters-headline">{{'Active filters'}}</h4>
  
    </div>
</div>

当所有过滤器项被移除后,active-filters 组件将不会出现。因为activeFilters.length 将是0。所以我不确定如何将aria-label 放在不存在的元素上。

这是可能引入屏幕阅读器逻辑的打字稿代码:

  ngAfterViewChecked() {
            if (this.interaction && this.interactionDataDelivery) {
                this.interaction = this.interactionDataDelivery = false;
                if (this.activeFilters.length == 0) {
                    //How do I set up the aria-label?
                }
            }
        }

【问题讨论】:

    标签: javascript html angular accessibility screen-readers


    【解决方案1】:

    我过去通过使用aria-live="assertive" 创建一个元素,使用some CSS trickery 将其隐藏(屏幕阅读器除外),然后设置其innerHTML 来完成此操作。大致如下:

    const $message = document.createElement('span');
    $message.classList.add('visuallyhidden'); // <-- Some method of making sure this element is only seen by screen readers
    $message.setAttribute('aria-live', 'assertive');
    
    document.body.appendChild($message);
    
    const speak = function (message) {
        $message.innerHTML = '';
        window.setTimeout(() => {
            $message.innerHTML = message;
        }, 100);
    };
    

    您可能还需要采取一些措施来确保使用屏幕阅读器的人也不能手动导航到警报消息元素,例如通过事后清除其内容或控制其aria-hidden 属性。

    【讨论】:

    • 谢谢你,它成功了!我想知道什么时候应该删除 visuallyhidden 类,以确保在屏幕阅读器宣布 aria-label 后将其删除?
    • 我使用该类的方式是防止元素在页面上可见,但仍允许屏幕阅读器看到它。因此,如果您只是在以这种方式使用它时删除该类,它只会导致该元素出现在页面上。你可能想在它不使用时给它display: none;,或者申请aria-hidden="true"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-02
    • 1970-01-01
    • 2016-11-11
    • 2010-09-06
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多