在iframe中获取活动元素

时间:2019-02-12 02:04:04

标签: reactjs typescript iframe

我的任务是在iframe中找到活动元素。

如果当前文档不是iframe,这是可以使用的代码

public componentWillMount(): void {
this._originalFocusedElement = getDocument()!.activeElement as HTMLElement;  }

对于iframe,我确实遇到了this堆栈溢出帖子,并尝试如下在我的代码中使用它

public componentWillMount(): void {
this._originalFocusedElement = getDocument()!.contentWindow.document.activeElement;
}

但是,这不能编译。

关于如何在iframe中找到活动元素的任何指导都是有用的。

1 个答案:

答案 0 :(得分:0)

  

但是,这不能编译

假设此编译:

this._originalFocusedElement = getDocument()!.activeElement as HTMLElement;

您未编译的代码中缺少as HTMLElement

固定代码

this._originalFocusedElement = getDocument()!.contentWindow.document.activeElement as HTMLElement;
相关问题