Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何将html内容附加到iframe中?_Javascript_Html_Angular - Fatal编程技术网

Javascript 如何将html内容附加到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

我尝试了下面的代码,但它完全替换了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: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);