简体   繁体   English

在 chrome 上删除元素时,Aria-live 区域不会读取更新

[英]Aria-live region is not reading out updates when an element is removed on chrome

When you dynamically add an element to an aria-live region, Chrome will read out all the items in that region which is great.当您将元素动态添加到 aria-live 区域时,Chrome 会读出该区域中的所有项目,这很棒。

But when you remove an element, Chrome does not re-read out the list.但是当您删除一个元素时,Chrome 不会重新读出该列表。 This is an issue when you're using the region for errors and such, as when the user has fixed an error, the list is not re-read out.当您将区域用于错误时,这是一个问题,例如当用户修复错误时,列表不会重新读出。

Example here: https://codepen.io/mildrenben/pen/WNNzVzN?editors=1010此处示例: https://codepen.io/mildrenben/pen/WNNzVzN?editors=1010

<div aria-live='assertive'>

</div>

<button id='add'>add</button>
<button id='remove'>remove</button>
const addBtn = document.querySelector('#add')
const removeBtn = document.querySelector('#remove')
const ariaLive = document.querySelector('div')


let tick = 0

addBtn.addEventListener('click', () => {
  let newElem = document.createElement('span')
  newElem.textContent = tick
  tick++
  console.log(ariaLive, newElem)
  ariaLive.appendChild(newElem)
})

removeBtn.addEventListener('click', () => {
  ariaLive.removeChild(ariaLive.lastChild)
})

The correct method should be to use the aria-relevant attribute, however the browser support is very poor, and as such it is not reliable.正确的方法应该是使用aria-relevant属性,但是浏览器支持很差,不可靠。

I don't normally advocate for doing hacky things to make a browser behave a certain way, but if you really need to make the live region report removals, here's what I would suggest:我通常不提倡做一些骇人听闻的事情来让浏览器以某种方式运行,但如果你真的需要让实时区域报告删除,我会建议以下内容:

  1. Set the aria-atomic attribute on your live region to true .将活动区域上的aria-atomic属性设置为true This means that the screen reader will read the entire contents of the live region each time content is added (but not removed).这意味着屏幕阅读器将在每次添加(但不删除)内容时读取活动区域的全部内容。
  2. When you delete an element from the live region, add another invisible element, wait a few hundred milliseconds, and then delete that element.当您从活动区域中删除一个元素时,添加另一个不可见元素,等待几百毫秒,然后删除该元素。
  3. The live region should announce all of the contents (minus the deletion) when the remove button is pressed.按下删除按钮时,活动区域应宣布所有内容(减去删除)。

Example fiddle here: https://jsfiddle.net/mug6vonf/3/此处的小提琴示例: https://jsfiddle.net/mug6vonf/3/

You should also use aria-relevant :您还应该使用aria-relevant

Values:价值观:

A space-delimited list of one or more of the following values:以下一个或多个值的空格分隔列表:

  • additions are insertion of nodes into the live region; additions是将节点插入活动区域; should be considered relevant.应该被认为是相关的。
  • removals are deletion of nodes; removals是删除节点; should be considered relevant.应该被认为是相关的。
  • text are changes to the textual content of existing nodes; text是对现有节点的文本内容的更改; should be considered relevant.应该被认为是相关的。
  • all is equivalent to additions removals text. all相当于添加删除文本。

aria-relevant="additions text" is the default value on a live region. aria-relevant="additions text"是活动区域的默认值。

The default value doesn't include removals , which you probably need.默认值不包括您可能需要的removals

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

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