简体   繁体   English

Angular2-如何在从DOM中删除元素时降低焦点并重新关注添加的新元素

[英]Angular2 - How to drop focus while element is being removed from DOM and refocus on new element added

I am using an *ngIf statement to switch on and off panels within an accordian element. 我正在使用*ngIf语句来打开和关闭*ngIf元素内的面板。 For Accessibility requirements, when one panel closes (by pressing the continue button to move to the next) and the next one opens I need focus to land onto a <legend> that announces a title for the next panel. 对于可访问性要求,当一个面板关闭(通过按继续按钮移动到下一个面板)而下一个面板打开时,我需要集中精力进入<legend> ,该名称为下一个面板声明标题。 The screen reader (NVDA) does read the text, but before it does it reads four lines that I don't expect. 屏幕阅读器(NVDA)确实会读取文本,但是在读取之前,它会读取我不希望的四行。 (replace website title with the name of the site) It reads off: (用网站名称替换网站标题 )它显示为:

  • website title - Mozilla Firefox 网站标题 -Mozilla Firefox
  • unknown 未知
  • website title - Mozilla Firefox 网站标题 -Mozilla Firefox
  • website title - document 网站标题 -文档

then it finally reads the title of the next section. 然后它最终读取下一部分的标题。 The reason it is doing that is because I have some angular2 collapse/expand animation that is opening/closing panels. 这样做的原因是因为我有一些angular2折叠/展开动画正在打开/关闭面板。 While the animation is happening, it can't focus to the next panel's <legend> and by that being delayed, it reads off the 4 lines of unwanted text. 动画发生时,它无法聚焦到下一个面板的<legend>并且由于延迟而无法读取4行不需要的文本。

The only solution I can think of is something like when a panel collapses I put focus on an offscreen empty div that won't read off anything and then reset the focus onto the subtitle when the animation is done, but that seems kind of hacky. 我能想到的唯一解决方案是,当面板折叠时,我将焦点放在屏幕外的空div上,该div不会读取任何内容,然后在完成动画后将焦点重新设置到字幕上,但这似乎有点不客气。 Are there any other solutions out there for this problem? 还有其他解决方案可以解决此问题吗?

put Queries in component 将查询放在组件中

 @Component({
   queries : {
      vc : new ng.core.ViewChildren('input') // depends on what you are adding dynamically, replace that with 'input'
   }
 })

in class add this 在课堂上添加

 ngAfterViewInit: function() {

    this.vc.changes.subscribe(elements => {
       elements.last.nativeElement.focus();
    });

}

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

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