简体   繁体   English

如何使用WAI-ARIA通知屏幕阅读器现在可以看到div

[英]How to notify screen readers using WAI-ARIA that a div is now visible

How do you notify screen readers using WAI-ARIA that a div is now visible? 如何使用WAI-ARIA通知屏幕阅读器现在可以看到div?

If we got the html 如果我们得到了HTML

<div id="foo">Present main content</div>
<div id="bar" style="display: none;">Hidden content</div>

and then we 然后我们

$('#foo').hide();
$('#bar').show();

how do we notify screen readers that they should notify the user about the now visible div (or possibly automatically focus on the now visible div)? 我们如何通知屏幕阅读器他们应该通知用户现在可见的div(或者可能自动关注现在可见的div)?

You do not need generally to tell screen readers that content is now visible. 您通常不需要告诉屏幕阅读器现在可以看到内容。 Use of aria-hidden makes no difference in practice, so I would suggest not using it. 使用aria-hidden在实践中没有区别,所以我建议不要使用它。 If you want the text content of the hidden div to be announced by a screen reader you may use role=alert or aria-live=polite (for example). 如果您希望屏幕阅读器宣布隐藏div的文本内容,您可以使用role=alertaria-live=polite (例如)。 You would use this for updated content that you want a screen reader to hear without having to move to the content location to discover it. 您可以将此用于您希望屏幕阅读器听到的更新内容,而无需移动到内容位置来发现它。 For example a pop up message that does not receive focus, but contains text information that is relevant to a user after an action such as pressing a button. 例如,弹出消息不接收焦点,但包含在诸如按下按钮之类的动作之后与用户相关的文本信息。

update: I discussed with one of the people who developed ARIA 1.0, he suggested using HTML5 hidden instead of aria-hidden as a semantic indication that content is hidden. 更新:我与开发ARIA 1.0的人讨论过,他建议使用HTML5 hidden而不是aria-hidden作为隐藏内容的语义指示。 use it in conjunction with CSS display:none for older browsers. 与旧版浏览器的CSS display:none结合使用。 Browsers that support HTML5 hidden implement it using display:none in the user agent style sheet. 支持HTML5 hidden浏览器使用用户代理样式表中的display:none来实现它。

Tagging the content with role="alert" will cause it to fire an alert event which screen readers will respond to when it becomes visible: 使用role =“alert”标记内容将导致它触发一个警报事件,屏幕阅读器将在其可见时响应:

<div id="content" role="alert">
...
</div>

$("#content").show();

This would alert the user when #content becomes visible. 当#content变得可见时,这将提醒用户。

aria-hidden should be used when there is something you want to hide from the screen reader, but show it to sighted users. 当您想要从屏幕阅读器中隐藏某些内容时,应使用aria-hidden,但应向有视力的用户显示。 Use with care however. 但要小心使用。 See here for more: http://www.456bereastreet.com/archive/201205/hiding_visible_content_from_screen_readers_with_aria-hidden/ 有关更多信息,请参阅此处: http//www.456bereastreet.com/archive/201205/hiding_visible_content_from_screen_readers_with_aria-hidden/

Use aria-hidden 使用咏叹调隐藏

Indicates that the element and all of its descendants are not visible or perceivable to any user as implemented by the author. 表示作者实现的元素及其所有后代对于任何用户都不可见或不可感知。 See related aria-disabled. 见相关的咏叹调。

If an element is only visible after some user action, authors MUST set the aria-hidden attribute to true. 如果元素仅在某些用户操作后可见,则作者必须将aria-hidden属性设置为true。 When the element is presented, authors MUST set the aria-hidden attribute to false or remove the attribute, indicating that the element is visible . 当元素出现时,作者必须将aria-hidden属性设置为false或删除属性,指示元素是可见的 Some assistive technologies access WAI-ARIA information directly through the DOM and not through platform accessibility supported by the browser. 一些辅助技术直接通过DOM访问WAI-ARIA信息,而不是通过浏览器支持的平台可访问性。 Authors MUST set aria-hidden="true" on content that is not displayed, regardless of the mechanism used to hide it. 无论用于隐藏它的机制如何,作者必须在未显示的内容上设置aria-hidden =“true”。 This allows assistive technologies or user agents to properly skip hidden elements in the document. 这允许辅助技术或用户代理正确地跳过文档中的隐藏元素。

so your code could become 所以你的代码可能会成为

$('#foo').hide().attr('aria-hidden', 'true');
$('#bar').show().attr('aria-hidden', 'false');

I created a sample showing how you could using role="alert" to notify screen reader users when they are approaching the character limit of a textarea element, if you are trying to understand how to use the alert role, it may help: 我创建了一个示例,展示了如何使用role =“alert”在屏幕阅读器用户接近textarea元素的字符限制时通知他们,如果您正在尝试了解如何使用警报角色,它可能会有所帮助:

There's more to it, but here's the small part of the code which produces the alert: 还有更多,但这里是产生警报的代码的一小部分:

if (characterCount > myAlertTheshold) {
       var newAlert = document.createElement("div"); /* using the native js because it's faster */
       newAlert.setAttribute("role", "alert");
       newAlert.setAttribute("id", "alert");
       newAlert.setAttribute("class","sr-only");
       var msg = document.createTextNode('You have ' + charactersRemaining +' characters of ' + myMaxLength + ' left');
       newAlert.appendChild(msg);
       document.body.appendChild(newAlert);
     }

http://codepen.io/chris-hore/pen/emXovR http://codepen.io/chris-hore/pen/emXovR

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

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