Javascript 替换iframe文档的HTML源

Javascript 替换iframe文档的HTML源,javascript,jquery,html,iframe,Javascript,Jquery,Html,Iframe,我正在处理一个显示数据库字段内容的页面。内容为HTML格式,包含整个网页 <html> <title>Some title</title> <style type="text/css"> /* CSS content here */ </style> <body> <!-- body content here --> </body> </html>

我正在处理一个显示数据库字段内容的页面。内容为HTML格式,包含整个网页

<html>
  <title>Some title</title>
  <style type="text/css">
     /* CSS content here */
  </style>
  <body>
    <!-- body content here -->
  </body>
</html>
我的网页包含一个名为iframe_container的div,其中包含一个iframe

<div id="iframe_container"><iframe/></div>

我知道这是因为我正在iframe中找到html标记并更改html。有没有一种方法可以使用jQuery覆盖整个文档?

您不需要jQuery,只需使用
document.write
将内容写入iframe即可完全替换iframe的文档

例如(纯香草JS)


打开文档并随后关闭将通过追加内容解决问题

var iframe = document.getElementById('iframeID');
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlData);
iframe.contentWindow.document.close();

这不会替换,而是附加到iframe文档
<html>
  <html>
    <title>Some title</title>
    <style type="text/css">
       /* CSS content here */
    </style>
    <body>
      <!-- body content here -->
    </body>
  </html>
</html>
document.getElementById("myIframeId").contentWindow.document.write(myHTML);
var iframe = document.getElementById('iframeID');
iframe.contentWindow.document.open();
iframe.contentWindow.document.write(htmlData);
iframe.contentWindow.document.close();