如何检查输入字段是否在Angular 7中处于焦点 [英] How to check if an input field is in focus in Angular 7

查看:105
本文介绍了如何检查输入字段是否在Angular 7中处于焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,我想知道表单中的任何输入字段是否集中?

I have a form and I want to know if any of the input fields in the form are focused or not?

我阅读了' NgForm '文档,但是没有找到与' focus '相关的任何内容.

I read the 'NgForm' documentation but didn't find anything related to 'focus'.

我发现感动,但不能满足需要.

I found touched but it doesn't satisfy needs.

推荐答案

您可以使用焦点和模糊事件来跟踪字段获得或失去焦点时的情况:

You can use the focus and blur events, to track as fields gain or lose focus :

<input (focus)="onFocus()" (blur)="onBlur()">

还有javascript自己的内容:

There are also javascript’s own :

document.hasFocus():文档或文档中的任何元素是否具有焦点.

document.hasFocus() : whether the document or any element inside the document has focus.

document.activeElement:包含当前具有焦点的元素的属性.

document.activeElement : Property containing which element currently has focus.

这篇关于如何检查输入字段是否在Angular 7中处于焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆