【问题标题】:is it possible to set focus for element?是否可以为元素设置焦点?
【发布时间】:2020-02-20 20:25:14
【问题描述】:

我在页面上有两个元素:

 <input tabindex="0" type="text" (keydown)="keydownInputSearch($event)" (click)="showSearchResultContent = !showSearchResultContent" #inputSearch>

并阻止:

 <div #searchList class="SearchList" *ngIf="showSearchResult()" tabindex="-1"></div>

问题是当块#searchLis 存在时,我无法将焦点设置回#inputSearch

我试过了:

 this.inputSearch.nativeElement.focus();

没有错误,没有效果。我想问题是#searchListtabindex="-1"#inputSearch 更少:tabindex="0"

但默认情况下,输入应该自动获得焦点。

【问题讨论】:

标签: angular angular6 angular8


【解决方案1】:

你快到了。只需关注ngAfterViewInit中的输入

 ngAfterViewInit() {
   this.inputSearch.nativeElement.focus();
 }

Stackblitz:https://stackblitz.com/edit/hello-angular-6-1svrgt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    • 2020-10-20
    • 1970-01-01
    • 1970-01-01
    • 2022-12-03
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多