我尝试了下面的代码,但它完全替换了 iframe 中的内容:
@ViewChild('iframe') iframe: ElementRef;
let content = '<h1>TEST CONTENT</h1>';
let doc = this.iframe.nativeElement.contentDocument;
doc.open();
doc.write(content);
doc.close();
如何在不替换的情况下将 html 内容附加到 iframe 中?
最佳答案
this.iframe.nativeElement.contentWindow.frames.document.body.insertAdjacentHTML('beforebegin', contentToAppend);
这对我有用。我使用 ElementRef
和 @ViewChild
在 Angular 中获取 iframe 元素,然后挖掘它的 HTML 主体并使用 insertAdjacentHTML
方法附加 HTML 内容,在所需的位置,可用作 beforebegin
、afterbegin
、beforeend
和 afterend
。
关于javascript - 如何将html内容附加到iframe中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52737867/