替换Iframe的整个内容

有没有办法用Javascript(或Jquery)替换iframe的整个内容?

例如,如果我的iframe有以下内容……

blah outside of html     

my title

blah outside of html

..它可以全部由你的脚本替换:)

到目前为止我见过的最好的是:

 $(youriframe).contents().find('html').html('your content here'); 

但是仍然不会替换html标签之外的内容。 我们有一个有趣的场景,我们必须确保所有内容都被替换。

或者创建一个包含新内容的新iframe,并简单地替换以前的iframe。

请不要评论src属性。

我有同样的问题,如果我使用这样的代码:

 $(youriframe).contents().find('html').html('your content here'); 

它在Chrome或FireFox中运行良好,但在IE8中,不显示任何内容或iframe中的滚动条不会出现。

如果我使用Rachel的方法,iframe内容在所有浏览器中都是这样的:

  

有没有其他解决方案?

尝试过这个:

 var doc = document.getElementById(iframeId).contentWindow.document; doc.open(); doc.write(iframeContent); doc.close(); 

这适用于我的情况,ie8和FF / chrome。

我猜你在这个例子中没有遇到跨站点脚本问题…你有没有运气使用contentWindow? 这样的事情对我来说似乎运作得很好:

 iframe = document.getElementById('iframe_id'); iframe.contentWindow.document.open() iframe.contentWindow.document.write(new_content_here); 

不是我的想法,在这里找到它: 使用Javascript或jQuery将元素写入子iframe

我有一个类似的场景,每次点击一行表时,我都会生成一个包含来自不同域的内容的新iFrame。

为此,我使用jQuery AJAX和PHP。 PHP查看我的MySQL表并获取我想加载的iFrame的URL; 它为iFrame生成HTML,我使用jQuery AJAX来替换该内容。

我的HTML看起来像这样:

 

我的AJAX调用如下所示:

 function loadPage(record_id){

     $就({
        类型:“POST”,
         url:“php / ajax_handler.php”,
        数据:“action = makeIFrame&record_id =”+ record_id,
        成功:函数(msg){
             $('。xFrame')。html(msg);
         }
     });
 }

该代码使用新的iFrame替换包含iFrame的div的HTML。

希望这(或类似的东西)适合你。