简体   繁体   English

屏幕阅读器和Javascript

[英]Screen readers and Javascript

I'm creating a website for a reading service for the blind and visually impaired and I'm using JavaScript (with jQuery) to print some stuff to some of the pages after the page has loaded. 我正在为盲人和视障人士的阅读服务创建一个网站 ,我正在使用JavaScript(使用jQuery)在页面加载后将一些内容打印到某些页面。

Will the screen reader read the content that is printed to the page with jquery after the page has been loaded? 屏幕阅读器是否会在页面加载后读取使用jquery打印到页面的内容?

From this page - "Generally, [screen readers] access the DOM (Document Object Model), and they use browser APIs (Application Programming Interfaces) to get the information they need." 这个页面 - “通常,[屏幕阅读器]访问DOM(文档对象模型),他们使用浏览器API(应用程序编程接口)来获取他们需要的信息。”

and we know that jQuery is a DOM manipulation library. 我们知道jQuery是一个DOM操作库。

So the question becomes.. do screen readers take a copy of the whole DOM and then parse it and read it? 所以问题变成..屏幕阅读器是否会获取整个DOM的副本,然后解析并阅读它? Or do they read the DOM, the same one that jQuery works on? 或者他们是否读取了与jQuery相同的DOM?

Here's an example of one of the pages that I use JavaScript on. 这是我使用JavaScript的其中一个页面的示例。 It uses a function that determines what program we have playing over the air and then prints the name of the program and a link to listen to it. 它使用一种功能来确定我们在空中播放的节目,然后打印节目的名称和听取它的链接。

<div id="now-playing-div"></div>

<script>

// invoke the audio-reader javascript library
$( document ).ready( function() {
  var callback = nowPlaying; // catalog, schedule, podcasts, archive or nowPlaying
  var selector = '#now-playing-div';
  makeAudioReaderPage(callback, selector);
});

</script>

So as you can see, if the screen reader doesn't read what the javascript/jquery prints to the #now-playing-div then it will read nothing. 正如您所看到的,如果屏幕阅读器没有读取javascript / jquery打印到#now-playing-div的内容,那么它将不会读取任何内容。 Then, we started getting a few emails of confused listeners wondering what happened to the Now Playing link. 然后,我们开始收到一些混乱的听众的电子邮件,想知道正在播放链接发生了什么。

So this morning I added this: 所以今天早上我添加了这个:

<div id='no-js'>Please enable JavaScript to receive this content.</div>

<script>
$( document ).ready( function() {
  $('#no-js').toggle();
});

</script>

But if the problem isn't that JavaScript needs to be enabled ( a recent survey shows that 99% of screen-reader users have JavaScript enabled) then the problem is not solved and is made even worse because now the screen reader user would think that JavaScript is not enabled. 但如果问题不在于需要启用JavaScript( 最近的一项调查显示 99%的屏幕阅读器用户启用了JavaScript),那么问题就无法解决,甚至更糟,因为现在屏幕阅读器用户会认为JavaScript未启用。

What to do?? 该怎么办??

You can test this without needing to know how screen readers parse the DOM. 您可以在不需要知道屏幕阅读器如何解析DOM的情况下对此进行测试。 I offer this answer primarily because you haven't offered any code to test ("some stuff to some of the pages" is not code to test) and your example doesn't provide enough context. 我提供这个答案主要是因为你没有提供任何代码来测试(“某些页面的某些东西”不是要测试的代码)而你的例子没有提供足够的上下文。

  • Install NVDA , a free screen reader for Windows. 安装NVDA ,一个用于Windows的免费屏幕阅读器。
  • If on a Mac, turn on VoiceOver. 如果在Mac上,请启用VoiceOver。
  • If on Ubuntu / Gnome, install Orca 如果在Ubuntu / Gnome上,请安装Orca
  • Download and run a trial of JAWS . 下载并运行JAWS试用版。
  • If on iOS, turn on VoiceOver. 如果在iOS上,请启用VoiceOver。
  • If on Android, turn on TalkBack. 如果在Android上,请启用TalkBack。
  • Heck, try Narrator on Windows. 哎呀,试试Windows上的讲述者。

There are plenty of tutorials to get you up and running. 有很多教程可以帮助您启动和运行。 Here are a couple: 这是一对夫妇:

Then, if you find that your script modifies the DOM after the screen reader has already parsed it, explore ARIA live regions and then look at browser support . 然后,如果您发现您的脚本在屏幕阅读器已经解析后修改了DOM,请浏览ARIA实时区域 ,然后查看浏览器支持

Finally, your example above about detecting if script is enabled doesn't actually need script to work if you use the <noscript> element: 最后,如果您使用<noscript>元素,上面关于检测脚本是否已启用的示例实际上并不需要脚本才能工作:

<noscript>
 <p>
 Please enable JavaScript to receive this content.
 </p>
</noscript>

If a browser has script enabled, this will not render (which is good). 如果浏览器启用了脚本,则不会呈现(这很好)。 This does not, however, address cases where the script block you want to show fails for other reasons (network lag, bugs, etc). 但是,这不会解决您希望显示的脚本块因其他原因(网络延迟,错误等)而失败的情况。 More on <noscript> 更多关于<noscript>

Since you say you are "creating a website for a reading service for the blind and visually impaired" the onus is really on you to learn the tools and how to test with them. 既然你说你“正在为盲人和视障人士创建一个阅读服务网站”,你就有责任学习工具以及如何用它们进行测试。

"Generally, [screen readers] access the DOM (Document Object Model), and they use browser APIs (Application Programming Interfaces) to get the information they need." “通常,[屏幕阅读器]访问DOM(文档对象模型),他们使用浏览器API(应用程序编程接口)来获取他们需要的信息。”

Modern screen readers use the Accessibility API. 现代屏幕阅读器使用Accessibility API。 They do not have to know anything about Javascript , or about the real DOM and HTML (with the sole exception of ChromeVox) . 他们不必了解任何有关Javascript ,或真正的DOM和HTML (ChromeVox除外)

For that reason, the same screenreader will be used to consult Internet with your favorite browser, to write mail with your favorite program (Thunderbird, Outlook, ...) or to play a flash player game, as long as those programs provide the correct informations to their Accessibility API. 出于这个原因,相同的屏幕阅读器将用于使用您喜欢的浏览器查询Internet,使用您喜欢的程序(Thunderbird,Outlook,...)编写邮件或玩Flash播放器游戏,只要这些程序提供正确的他们的Accessibility API的信息。 They don't parse the HTML directly but an accessible tree view of a document. 它们不直接解析HTML,而是解析文档的可访问树视图。

This means that if your browser understands javascript, your screenreader will. 这意味着如果您的浏览器了解javascript,您的屏幕阅读器将会。 That because, your screen reader will not access the DOM directly on the load of the page, but will interact with the Accessibility API provided by your browser. 这是因为,您的屏幕阅读器不会直接在页面加载中访问DOM,而是会与浏览器提供的Accessibility API进行交互。

Now, evidently, if your screenreader reads the content before it was modified by javascript, it won't have any clue about modifications made by javascript. 现在,显然,如果您的屏幕阅读器在javascript修改之前读取内容,它将不会对javascript所做的修改有任何线索。 For that matter, you can use aria-live attribute. 就此而言,您可以使用aria-live属性。

This is a short abstract, but the following article can give you more informations about the parsing of the DOM by screen readers: Why accessibility APIs matter 这是一个简短的摘要,但是下面的文章可以为您提供有关屏幕阅读器解析DOM的更多信息: 为什么可访问性API很重要

I needed to tell the screen reader that the page has changed and that it needs to read the content again. 我需要告诉屏幕阅读器页面已经更改,并且需要再次阅读内容。

This is done with the ARIA attributes. 这是通过ARIA属性完成的。

I've added the aria-live="polite" to my html. 我在我的html中添加了aria-live="polite" The "polite" setting tells the screen reader that it should read the new content but not interrupt what is currently being said. “礼貌”设置告诉屏幕阅读器它应该读取新内容但不会中断当前正在说的内容。 IIRC the other settings are off and assertive . IIRC其他设置是offassertive

<div id="now-playing-div" aria-live="polite"></div>

<script>

// invoke the audio-reader javascript library
$( document ).ready( function() {
  var callback = nowPlaying; // catalog, schedule, podcasts, archive or nowPlaying
  var selector = '#now-playing-div';
  makeAudioReaderPage(callback, selector); // the callback will append HTML to the element via jquery with the selector as the query term
});

</script>

Right now I'm having a guy who can't see anything test out the website, he used to be able to see and he learned to use computers before he lost his eye sight. 现在我有一个人看不到任何测试网站的东西,他曾经能够看到并且他在他失去视力之前学会了使用计算机。 He's having a hard time with the website unfortunately. 不幸的是,他在网站上遇到了困难。 The problem is that when he gets to the page to listen to the audio, he uses his keyboard shortcuts to search for the play button but there is no play button because the play button (jplayer) is in a popup window that pops up after clicking a link. 问题是,当他到达页面听音频时,他使用键盘快捷键搜索播放按钮,但没有播放按钮,因为播放按钮(jplayer)位于弹出窗口中,单击后弹出一条链接。 He gets frustrated and the phone call is over :( 他感到沮丧,电话结束了:(

So the good news is that he is able to read the content that is appended to the document via jquery, but the bad news is that there is bigger problems with the flow of the website and his expectations. 所以好消息是他能够通过jquery读取附加到文档中的内容,但坏消息是网站的流量和他的期望存在更大的问题。 This is my fault of course. 这当然是我的错。

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

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