简体   繁体   English

不使用document.write()将完整的html页面写入iframe

[英]Writing full html page into iframe without document.write()

I am creating an iframe dynamically using jquery, and I want to know if there is a way to write a full html page (html string response from an ajax request) into it without using the document.write() operation. 我正在使用jquery动态创建iframe,并且我想知道是否有一种方法可以在不使用document.write()操作的情况下将完整的html页面(来自ajax请求的html字符串响应)写入其中。

Since I'm starting off with an empty iframe the $.find().html() or innerHTML() methods are not going to work here. 由于我从一个空的iframe开始,因此$.find().html()innerHTML()方法在这里不起作用。 Also, iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html); 另外, iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html); doesn't work in all browsers. 不适用于所有浏览器。

Iframe: iframe:

  var myIframe = $('<iframe/>', {
                    id: 'my-iframe',
                    name: 'my-iframe',
                    src: 'about:blank',
                    scrolling: 'no',
                    frameborder: 0,
                    style: 'width: 224px; height: 230px;'
                });

Did you try this? 你有尝试过吗? After the iframe has been added to the parent document 将iframe添加到父文档后

// Edit 
myIframe.contents().find("body").html("My new content")

// Log
console.log(myIframe.contents().find("body").html())

you can use the .load function of jquery and load an html page into the iframe.. like this: 您可以使用jquery的.load函数并将html页面加载到iframe中。

$( "#my-iframe" ).load( "./path/to/the/html/file.html", function() {
  alert( "Load was performed." );
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM