单击按钮时,我想将焦点设置在页面顶部的div上。这意味着它会在单击按钮时进行验证,如果验证失败,则需要关注页面顶部显示验证错误的位置。

因此,如何获得焦点在页面div的顶部。形式为模态对话。

最佳答案



有关 tabIndex 的更多信息

用这样的东西

<div tabindex="1"></div>

甚至针对相同的内容制定指令,这将有助于您自定义功能
import { Directive, Input, ElementRef, Inject, OnChanges } from '@angular/core';

@Directive({ selector: '[focusField]' })
export class FocusDirective implements OnChanges {

  @Input('focusField') focusField: any;
  constructor(@Inject(ElementRef) private element: ElementRef) { }

  ngOnChanges(changes){
    if(changes.focusField.currentValue){
      this.element.nativeElement.focus();
    }
  }
}

07-24 09:43
查看更多