简体   繁体   English

如何在 primeng 对话框中将焦点设置在关闭按钮上

[英]How to set focus on close button in primeng dialog

How to set focus on close button in primeng dialog after it's opened?打开后如何在primeng对话框中将焦点设置在关闭按钮上? I tried something like this, but it doesn't work.我试过这样的东西,但它不起作用。 HTML: HTML:

<p-dialog
  [modal]="true"
  [resizable]="true"
  [draggable]="false"
  header="header"
  [(visible)]="dialogVisible"
  (onHide)="closeDialog()"
  (onShow)="openDialog()"
  focusOnShow="false"
>

ts: TS:

constructor(private element: ElementRef<HTMLAnchorElement>) {}
openDialog() {
    this.element.nativeElement.focus();
  }

Update: I found solution how to set focus on this element更新:我找到了如何将焦点放在这个元素上的解决方案

openDialog() {
    const element = this.elem.nativeElement.querySelectorAll('.ui-dialog-titlebar-close').item(0);
    element.focus();
console.log(document.activeElement)
  }

but it doesn't stick.但它不粘。 When I check focus inside openDialog it's set, but after that it still sets to another button.当我在 openDialog 中检查焦点时,它已设置,但之后它仍设置为另一个按钮。

I think you can use Renderer2 to access the element.我认为您可以使用 Renderer2 来访问该元素。 Something like this:像这样:

const element = this.renderer.selectRootElement('#elementId');

Then: element.focus();然后: element.focus();

I think you can refer here for better understanding.我想你可以参考这里以获得更好的理解。

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

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