[英]How to replace entire contents of iframe?
This question didn't receive any satisfactory answers, so I'm asking again. 这个问题没有得到任何满意的答案,所以我再问一遍。
I can get my iframe like this: 我可以这样得到我的iframe:
var iframe = document.getElementById('preview');
var win = iframe.contentWindow;
var doc = win.document;
And write some initial HTML to it like this: 并像这样写一些初始HTML:
doc.write('<!DOCTYPE html><html><head><link rel="stylesheet" type="text/css" href="reset.css"></head><body>blah blah</body></html>');
And that works great, but now I want to replace the entire thing with some new content. 这很有效,但现在我想用一些新内容替换整个内容。 How can I do that?
我怎样才能做到这一点?
To be clear, by "entire thing" I mean the <head>
too. 要明确,“整个事物”我的意思是
<head>
。 I need to link some external JS and CSS there. 我需要在那里链接一些外部JS和CSS。
Preferably, this would also include the <!DOCTYPE>
, but it's non-essential at this point. 优选地,这也包括
<!DOCTYPE>
,但此时它不是必需的。
您只需从DOM中删除旧的iframe节点,然后创建一个包含新内容的新节点。
我相信使用innerHTML应该有效:
doc.documentElement.innerHTML = "<script src='blah.js'></script><style></style>"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.