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

查看:22
本文介绍了如何检查输入字段是否在 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"文档,但没有找到与焦点"相关的任何内容.

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

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

I found touched but it doesn't satisfy needs.

推荐答案

您可以使用 focus 和 blur 事件来跟踪字段获得或失去焦点:

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天全站免登陆