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

时间:2016-06-27 18:57:13

标签: javascript jquery html iframe

我正在使用jquery动态创建iframe,我想知道是否有办法在不使用document.write()操作的情况下将完整的html页面(来自ajax请求的html字符串响应)写入其中。< / p>

由于我从一个空的iframe开始,$.find().html()innerHTML()方法在这里不起作用。此外,iframe.src = 'data:text/html;charset=utf-8,' + encodeURI(html);无法在所有浏览器中使用。

I帧:

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

2 个答案:

答案 0 :(得分:0)

你尝试过这个吗?将iframe添加到父文档后

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

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

答案 1 :(得分:0)

你可以使用jquery的.load函数并将一个html页面加载到iframe中..就像这样:

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