Javascript 如何将html内容附加到iframe中?
我尝试了下面的代码,但它完全替换了iframe中的内容:Javascript 如何将html内容附加到iframe中?,javascript,html,angular,Javascript,Html,Angular,我尝试了下面的代码,但它完全替换了iframe中的内容: @ViewChild('iframe') iframe: ElementRef; let content = '<h1>TEST CONTENT</h1>'; let doc = this.iframe.nativeElement.contentDocument; doc.open(); doc.write(content); doc.close(); @ViewChild('iframe')iframe:El
@ViewChild('iframe') iframe: ElementRef;
let content = '<h1>TEST CONTENT</h1>';
let doc = this.iframe.nativeElement.contentDocument;
doc.open();
doc.write(content);
doc.close();
@ViewChild('iframe')iframe:ElementRef;
让内容=‘测试内容’;
让doc=this.iframe.nativeElement.contentDocument;
doc.open();
文件编写(内容);
doc.close();
如何将html内容附加到iframe中而不替换?doc.open()所有文档的内容
您应该按照注释中的建议将文本添加为文本,或者将其附加到ex.正文中
doc.body.append(content)
这对我有用。我使用
ElementRef
和@ViewChild
以角度获取iframe元素,然后深入到它的HTML主体,并使用insertAdjacentHTML
方法将HTML内容附加到所需的位置,如beforebeagin
和afterbegin
所示,beforeed
和afterend
insertAdjacentHTML的工作方式与innerHTML类似,但比innerHTML更快。
元素。insertAdjacentHTML(位置,文本)
详情:
@ViewChild('iframe')iframe:ElementRef;
让doc=this.iframe.nativeElement.contentDocument;
文档insertAdjacentHTML('beforeed','testcontent');
我希望它能帮助您。您可以使用jquery Selector来附加内容,而不是使用此解决方案:
$("#specific_iframe_id").append(content);
您可以尝试以字符串形式获取iframe的内容,然后将
content
var附加到iframe中并编写它?可能类似于doc.body.innerHTML+=content代码>?不,OP在哪里提到jQuery?不是每个人都希望使用jQuery
'beforebegin': Before the element itself.
'afterbegin': Just inside the element, before its first child.
'beforeend': Just inside the element, after its last child.
'afterend': After the element itself.
@ViewChild('iframe') iframe: ElementRef;
let doc = this.iframe.nativeElement.contentDocument;
doc.insertAdjacentHTML('beforeend', '<h1>TEST CONTENT</h1>');
$("#specific_iframe_id").append(content);