gpt4 book ai didi

iframe - 更改 Angular 2中iframe内元素的属性

转载 作者:行者123 更新时间:2023-12-02 03:02:22 35 4
gpt4 key购买 nike

我想在 Angular 2 Typescript 中更改 iframe 内元素的属性,类似于 JavaScript 代码

document.getElementById('iframeId').window.document.getElementById('home-grid').style.visibility = "隐藏";

我的 Angular typescript 代码:

var iframe   = document.getElementById('iframeId');
var insideDoc = iframe.contentDocument || iframe.contentWindow.document;

编译代码时出错:

stream.js:74
throw er; // Unhandled stream error in pipe.
^
Error: ./angularapp/web/component/mainPage/mainPage.ts
←[37m(←[39m←[36m35←[39m,←[36m32←[39m): ←[31merror TS2339: Property 'contentDocument' does not exist on type 'HTMLElement'.←[39m./angularapp/web/component/mainPage/mainPage.ts
←[37m(←[39m←[36m35←[39m,←[36m58←[39m): ←[31merror TS2339: Property 'contentWindow' does not exist on type 'HTMLElement'.←[39m

有什么办法可以在 Angular 2 中实现这一点吗?请帮忙

最佳答案

@Component({
selector: 'my-app',
template:`
<h1>Selecting Number</h1>
<iframe id="iframeId" src="iframe.html" (load)="onLoad()"></iframe>
`,
})
export class App {
onLoad() {
var iframe = document.getElementById('iframeId');
var iWindow = iframe.contentWindow
var doc = iframe.contentDocument || iframe.contentWindow.document;
console.debug(doc);
console.log(doc.getElementById('foo').innerText);
}
}

Plunker demo

关于iframe - 更改 Angular 2中iframe内元素的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36513929/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com