如何将HTML内容设置到iframe中

48

我有一个HTML字符串

<html>
  <body>Hello world</body>
</html> 

我想用JavaScript将其设置为iframe。我正在尝试像这样设置HTML:

contentWindow.document.body.innerHTML
或者
contentDocument.body.innerHTML
或者
document.body.innerHTML

但是 IE 给出 "Access is denied." 或 "Object does not support this property or method." 或者 "Invalid final element to the action." 错误。

这是我完整的代码:

<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="jquery_1.7.0.min.js"/>
    <script type="text/javascript">
      $(document).ready(function(){
        var htmlString = "<html><body>Hello world</body></html>";
        var myIFrame = document.getElementById('iframe1');
        // open needed line commentary
        //myIFrame.contentWindow.document.body.innerHTML = htmlString;
        //myIFrame.contentDocument.body.innerHTML = htmlString;
        //myIFrame.document.body.innerHTML = htmlString;
        //myIFrame.contentWindow.document.documentElement.innerHTML = htmlString;
      });
    </script>
  </head>
  <body>
    <p>This is iframe:
      <br>
      <iframe id="iframe1">
      <p>Your browser does not support iframes.</p>
      </iframe>
  </body>
</html>

1
为什么你不加载页面,它很好用呀?为什么要使用字符串来加载?告诉我吧。 - The Mechanic
7个回答

67

你可以使用:

document.getElementById('iframe1').contentWindow.document.write("<html><body>Hello world</body></html>");

这里有一个jsFiddle,它可以在所有主要浏览器中运行。

请注意,您应该使用contentWindow.document.write而不是contentDocument.write:这使其在IE7中也能正常工作。


1
请注意,如果您正在读写iframe中的内容,则需要编写document.getElementById('iframe1').contentWindow.document.write("<html><body></body></html>");以使空字符串读取如下所示 var iframe_html = document.getElementById('iframe_exe_uploader').contentWindow.document.body.innerHTML; - Yevgeniy Afanasyev
4
我需要使用open/close在Chrome浏览器中使其正常工作: iframe.contentWindow.document.open('text/htmlreplace'); iframe.contentWindow.document.write(content); iframe.contentWindow.document.close(); - Henry
2
如果您不想追加,可以使用document.getElementById('output_iframe1').src = "data:text/html;charset=utf-8," + escape(html_string);来实现。(来源) - user1717828

27
var htmlString="<body>Hello world</body>";
var myIFrame = document.getElementById('iframe1');
myIFrame.src="javascript:'"+htmlString+"'";

有了html5,您将能够使用srcdoc属性


9

innerHTML在IE中有一些棘手的问题,特别是像thead这样的元素是只读的,会引起很多麻烦。

根据msdn的文档,您可以尝试使用documentMode,它提供了一个innerHTML属性。

myIFrame = myIFrame.contentWindow ||
    myIFrame.contentDocument.document ||
    myIFrame.contentDocument;
myIFrame.document.open();
myIFrame.document.write('Your HTML Code');
myIFrame.document.close();

这可能只在IE中有效。


4
在2023年,解决此问题的正确方法是使用<iframe>元素的srcdoc属性。这可以直接在您的HTML文件中完成,也可以使用javascript完成:
document.getElementById('iframe').srcdoc = "<html><body>Hello world!</body></html>";

0

如何使用document.documentElement.innerHTML呢?但要知道,页面中的所有内容都将被替换,包括执行此操作的脚本。

对于一个iframe,可以这样使用:myIFrame.contentWindow.document.documentElement.innerHTML


1
谢谢您的回答,但是如果我使用这段代码,会出现以下错误:“操作的最终元素无效。” - SBotirov

0

我在这里看到的答案中,对于“origin”我有些疑惑。对我来说,它的工作方式如下:

const frame1 = document.createElement('iframe');
frame1.name = 'frame1';
//not have to set this style,just for demo
frame1.style.position = 'absolute';
frame1.style.height = '800px';
frame1.style.top = '100px';
frame1.style.left = '100px';
frame1.style.background = 'white';

document.body.appendChild(frame1);
const frameDoc =
  frame1.contentWindow || frame1.contentDocument.document || 
  frame1.contentDocument;

frameDoc.document.write('<html><head><title></title><body>Hello world</body></html>');
frameDoc.document.close();

-4

试一下:

$('iframe').load(function() {
    $(this).contents().find('body').append("Hello world");
}); 

更新:

$(function(){
    $('iframe').load(function() {
        $(this).contents().find('body').append("Hello world");
    }); 
})

期望使用原生JS来回答问题,但是这个例子使用了jQuery。 - jtrezza

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接