我无法检查文本区域是否聚焦。我将尝试使用以下代码,但它不起作用。
const msg_area = document.getElementById('msg-area') as HTMLInputElement;
if (msg-area.hasFocus()) {
// somecode
}
但这不起作用。出现错误,找不到 hasFocus()。 hasFocus() 不是 HtmlInputElement 的属性。
document.activeElement
属性将指向文档中当前聚焦的元素,因此您可以编写:
if(document.activeElement === msgArea) {
// The `msgArea` textarea element currently has focus
}