在iframe中将变量设置为活动元素

时间:2018-12-19 23:46:38

标签: typescript

我想将原始焦点元素设置为iFrame中活动元素的焦点。这是我原来的功能。

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

// My attempt
public componentWillMount(): void {
this._originalFocusedElement = i.getDocument()!.activeElement!.contentWindow.document.activeElement
}

但是它给了我编译错误

[ts] Property 'contentWindow' does not exist on type 'Element'. [2339]

有人可以建议我如何将_originalFocusedElement设置为iframe中的活动元素吗?

1 个答案:

答案 0 :(得分:0)

大多数HTMLElement都没有contentWindow属性。尝试将其转换为HTMLIFrameElement

const iframe = i.getDocument()!.activeElement! as HTMLIFrameElement;
this._originalFocusedElement = iframe.contentWindow.document.activeElement;