javascript - 如何让屏幕阅读器响应显示和隐藏动态 Web 应用程序中的内容?

标签 javascript html accessibility screen-readers

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

举个例子:

<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>

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

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

然而,在组件之间切换时,NVDA 似乎不会读取组件的内容。有什么办法可以实现吗?

Please see this Gist on GitHub to check using NVDA

最佳答案

将这两个 block 填充到 ARIA live region 中.

有一些live region properties你需要知道让它发挥作用。如果你希望他们一改变就被宣布,不管用户在做什么,那么它就是assertive。如果您希望它等到用户完成交互,那么它将是礼貌的。有corresponding live region roles同样,我在下面展示了这一点。

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

<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>

此外,aria-atomic属性将告诉屏幕阅读器它是否应该宣布整个事情(这对警告消息很有用)或只宣布改变的部分(这可能更适合计时器)。

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

除了 NVDA,这还将解决屏幕阅读器问题。

这是一个 example of an offline alert .这是一个方便的 slideshare that goes into more detail .既然您知道要搜索什么,那么还有很多。

关于javascript - 如何让屏幕阅读器响应显示和隐藏动态 Web 应用程序中的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40766358/

相关文章:

html - 强制 [innerHTML] 内容适应 div 宽度

javascript - 静态定位的div框问题

html - 具有特殊按键事件的元素的 ARIA 处理

javascript - 如何使用 Jquery 获取特定 Id 的所有选定下拉值

javascript - 如何使表格中的整行像ngtable(angularjs)中的超链接(在新选项卡中打开等)一样

javascript - 我应该将我的 javascript 拆分成多个文件吗?

pdf - 提供大量 PDF 的网站的可访问性问题

accessibility - 使用 JAWS 获取关键事件

javascript - 在 Node.JS 中跨数组迭代保存

javascript - 如何使用TideSDK启动steam游戏?