使用jQuery从原始页面内容创建iframe

Create iframes from original page content with jQuery

本文关键字:创建 iframe jQuery 原始 使用      更新时间:2023-09-26

我正在尝试将整个页面动态地"重写"为一组iframe。我想要在iframe底部有一个音乐播放器栏,并在上面/后面的iframe中"重写"原始页面。几乎和SMC球员一样。我觉得我很接近,但我是一个jQuery新手。。因此,任何指示都将在事前得到极大的赞赏。

以下是创建无法正常工作的iframe的主要部分。

var oldpage =  "<html>" + $("html").html() + "</html>";
$('body').html( "<iframe frameborder='0' id='content' allowtransparency='true' name='content'></iframe>" );
$('#content').contents().find('html').html(oldpage);
$('body').append("<iframe id='stratus' allowtransparency='true' frameborder='0' scrolling='0'>");

我正试图在第1行加载整个原始html。然后创建iframe并尝试将oldbody变量注入到新的iframe#内容中。iframe已创建,但#content iframe的正文为空。

我不知道为什么,但你不能替换整个HTML元素,只能替换它的内容:

// create raw frame
var frame = $('<iframe id="maincontent" style="position:absolute; top: 0; left: 0; width: 50%; height: 50%;"><html></html></iframe>');
// store a copy of current document contents
var copiedHead = $('head').clone();
var copiedBody = $('body').clone();
// empty the current document
$(':not(iframe) body *').remove();
// load the copy into the frame;
// it's not possible to replace the whole HTML element;
// using plain JS DOM function here, since jQuery would strip out any SCRIPT and STYLE tags
frame.load(function(){
    $(this).contents().find('html')[0].replaceChild(copiedHead[0] , $(this).contents().find('head')[0]);
    $(this).contents().find('html')[0].replaceChild(copiedBody[0] , $(this).contents().find('body')[0]);
}).appendTo('body');