如何让屏幕阅读器响应动态

如何让屏幕阅读器响应动态

本文介绍了如何让屏幕阅读器响应动态 Web 应用程序中的显示和隐藏内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个可访问的网页,其中包含许多可以在用户与页面交互时隐藏和显示的组件.当显示组件时,我希望屏幕阅读器(在本例中为 NVDA)读取组件的内容.

举个例子:

<身体><div id="comp1" style="display:none;"role="region" tabindex="-1" aria-expanded="false"><div>这是组件 1</div><button type="button" onclick="ShowComponent(comp2)">显示2</button>

<div id="comp2" style="display:none;"role="region" tabindex="-1" aria-expanded="false"><div>这是组件 2</div><button type="button" onclick="ShowComponent(comp1)">显示1</button>

<script type="text/javascript">函数隐藏所有(){//隐藏两个组件var comp1 = document.getElementById("comp1");comp1.style.display = "无";comp1.setAttribute("aria-expanded", "false");var comp2 = document.getElementById("comp2");comp2.style.display = "无";comp2.setAttribute("aria-expanded", "false");}功能 ShowComponent(comp) {全部藏起来();//显示这个组件comp.style.display = "块";comp.setAttribute("aria-expanded", "true");comp.focus();}ShowComponent(document.getElementById("comp1"));</html>

在上面的示例中,单击组件 1 中的按钮将隐藏组件 1 并显示组件 2.

单击组件 2 中的按钮将隐藏组件 2 并显示组件 1.

然而,在组件之间切换时,NVDA 似乎不会读取组件的内容.有没有办法做到这一点?

请在 GitHub 上查看此要点以检查是否使用 NVDA

解决方案

将这两个块塞进一个 ARIA 直播区域.

您需要了解一些实时区域属性让它起作用.如果你希望他们一改变就被宣布,不管用户在做什么,那么它就是assertive.如果您希望它等到用户完成交互,那么它将是 polite.还有相应的实时区域角色,我将在下面展示.

我认为您不需要示例代码中的其他 ARIA 位或 tabindex,但我不知道页面的范围.这是一个不同的例子:

这里的任何内容将在更改后立即公布.

<div role="status" aria-live="polite">此处的任何内容都将在更改时公布,但仅在用户停止与页面交互后才会公布.

此外,aria-atomic 属性将告诉屏幕阅读器它是应该宣布整个事情(这对警报消息有好处)还是只宣布发生变化的部分(这可能更适合计时器).

避免在一个页面上有多个 ARIA 实时区域.

除 NVDA 外,这还将解决屏幕阅读器的问题.

这是一个离线警报示例.这是一个方便的幻灯片共享,详细介绍了.现在您知道要搜索的内容还有很多.

I would like to create an accessible web page which contains a number of components that can be hidden and shown as the user interacts with the page. When a component is shown I expect a screen reader (in this case NVDA) to read the contents of the component.

As an example:

<html>
<body>
	<div id="comp1" style="display:none;" role="region" tabindex="-1" aria-expanded="false">
		<div>This is component 1</div>
		<button type="button" onclick="ShowComponent(comp2)">Show 2</button>
	</div>
	<div id="comp2" style="display:none;" role="region" tabindex="-1" aria-expanded="false">
		<div>This is component 2</div>
		<button type="button" onclick="ShowComponent(comp1)">Show 1</button>
	</div>

	<script type="text/javascript">
		function HideAll() {
			// hide both components
			var comp1 = document.getElementById("comp1");
			comp1.style.display = "none";
			comp1.setAttribute("aria-expanded", "false");

			var comp2 = document.getElementById("comp2");
			comp2.style.display = "none";
			comp2.setAttribute("aria-expanded", "false");
		}

		function ShowComponent(comp) {
			HideAll();

			// show this component
			comp.style.display = "block";
			comp.setAttribute("aria-expanded", "true");
			comp.focus();
		}

		ShowComponent(document.getElementById("comp1"));
	</script>
</body>
</html>

In the above example clicking a button within component 1 will hide component 1 and show component 2.

Clicking a button within component 2 will hide component 2 and show component 1.

However NVDA does not seem to read the contents of the components when toggling between the components. Is there a way that this can be achieved?

Please see this Gist on GitHub to check using NVDA

解决方案

Stuff those two blocks into an ARIA live region.

There are some live region properties you need to know to make it work. If you want them to be announced as soon as they change, no matter what the user is doing, then it will be assertive. If you want it to wait until the user finishes an interaction, then it will be polite. There are corresponding live region roles as well, which I show below.

I do not think you need the other ARIA bits nor tabindex in your example code, but I do not know the scope of the page. Here is a different example instead:

<div role="alert" aria-live="assertive">
Anything in here will be announced as soon as it changes.
</div>

<div role="status" aria-live="polite">
Anything in here will be announced when it changes but only after the user has stopped interacting with the page..
</div>

Further, the aria-atomic property will tell the screen reader whether it should announce the entire thing (which is good for an alert message) or just the parts that changed (which might be a better fit for a timer).

Avoid having more than one ARIA live region on a page.

This will also address screen readers besides NVDA.

Here is an example of an offline alert. Here is a handy slideshare that goes into more detail. There is a lot more out there now that you know what to search.

这篇关于如何让屏幕阅读器响应动态 Web 应用程序中的显示和隐藏内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 16:56