如何检查文本区域是否获得焦点?

问题描述 投票:0回答:1

我无法检查文本区域是否聚焦。我将尝试使用以下代码,但它不起作用。

    const msg_area = document.getElementById('msg-area') as HTMLInputElement;
    if (msg-area.hasFocus()) {
        // somecode
    }

但这不起作用。出现错误,找不到 hasFocus()。 hasFocus() 不是 HtmlInputElement 的属性。

angular6
1个回答
0
投票

document.activeElement
属性将指向文档中当前聚焦的元素,因此您可以编写:

if(document.activeElement === msgArea) {
  // The `msgArea` textarea element currently has focus
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.