加载网页时,屏幕阅读器(如OS X附带的,或Windows上的JAWS)将读取整个页面的内容。但是请说您的页面是动态的,并且当用户执行操作时,新内容会添加到页面中。为简单起见,假设您在<span>
中的某处显示消息。如何让屏幕阅读器阅读新消息?
答案 0 :(得分:27)
WAI-ARIA规范定义了屏幕阅读器可以“观察”DOM元素的几种方式。支持的最佳方法是aria-live
属性。它有模式off
,polite
,assertive
和rude
。自信程度越高,就越有可能打断屏幕阅读器当前所说的内容。
以下内容已在Firefox 3和Firefox 4.0b9下使用NVDA进行了测试:
<!DOCTYPE html>
<html>
<head>
<script src="js/jquery-1.4.2.min.js"></script>
</head>
<body>
<button onclick="$('#statusbar').html(new Date().toString())">Update</button>
<div id="statusbar" aria-live="assertive"></div>
</body>
对于WAI-ARIA accomplished role="status"
和role="alert"
,同样可以roles。我有不兼容的报告,但无法重现它们。
<div id="statusbar" role="status">...</div>
答案 1 :(得分:1)
这是一个改编的现实世界的例子 - 这个高级标记已经从带有链接的无序列表转换为通过JS的选择菜单。真正的代码要复杂得多,显然不能完全包含在内,所以请记住,必须重新考虑这个代码才能用于生产。为了使选择菜单可以通过键盘访问,我们注册了按键和按键。当用户从列表中删除时,onchange事件并触发了AJAX调用(注意浏览器在onchange事件的时间上的差异)。这是一个可以访问的严肃的PITA,但它是可能的。
// HTML
<!-- select element with content URL -->
<label for="select_element">State</label>
<select id="select_element">
<option value="#URL_TO_CONTENT_PAGE#" rel="alabama">Alabama</option>
</select>
<p id="loading_element">Content Loading</p>
<!-- AJAX content loads into this container -->
<div id="results_container"></div>
// JAVASCRIPT (abstracted from a Prototype class, DO NOT use as-is)
var selectMenu = $('select_element');
var loadingElement = $('loading_element');
var resultsContainer = $('results_container');
// listen for keypress event (omitted other listeners and support test logic)
this.selectMenu.addEventListener('keypress', this.__keyPressDetector, false);
/* event callbacks */
// Keypress listener
__keyPressDetector:function(e){
// if we are arrowing through the select, enable the loading element
if(e.keyCode === 40 || e.keyCode === 38){
if(e.target.id === 'select_element'){
this.loadingElement.setAttribute('tabIndex','0');
}
}
// if we tab off of the select, send focus to the loading element
// while it is fetching data
else if(e.keyCode === 9){
if(targ.id === 'select_element' && targ.options[targ.selectedIndex].value !== ''){
this.__changeStateDetector(e);
this.loadingElement.focus();
}
}
}
// content changer (also used for clicks)
__changeStateDetector:function(e){
// only execute if there is a state change
if(this.selectedState !== e.target.options[e.target.selectedIndex].rel){
// get state name and file path
var stateName = e.target.options[e.target.selectedIndex].rel;
var stateFile = e.target.options[e.target.selectedIndex].value;
// get the state file
this.getStateFile(stateFile);
this.selectedState = stateName;
}
}
getStateFile:function(stateFile){
new Ajax.Request(stateFile, {
method: 'get',
onSuccess:function(transport){
// insert markup into container
var markup = transport.responseText;
// NOTE: select which part of the fetched page you want to insert,
// this code was written to grab the whole page and sort later
this.resultsContainer.update(markup);
var timeout = setTimeout(function(){
// focus on new content
this.resultsContainer.focus();
}.bind(this), 150);
}.bind(this)
});
}