【问题标题】:Getting active element inside an iframe在 iframe 中获取活动元素
【发布时间】:2019-02-12 02:04:04
【问题描述】:

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

这是在当前文档不是 iframe 时有效的代码

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

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

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

但是,这不会编译。

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

【问题讨论】:

标签: reactjs typescript iframe


【解决方案1】:

但是,这不会编译

假设编译:

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

您在无法编译的代码中缺少as HTMLElement

固定代码

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

【讨论】:

  • 第一个编译,但是,第二个给我,“[ts] Property 'contentWindow' does not exist on type 'Document'。”
猜你喜欢
  • 2016-05-22
  • 1970-01-01
  • 2010-11-08
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
相关资源
最近更新 更多