1

ページ全体を一連の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#contentに挿入しようとしています。iframeは作成されますが、#contentiframeの本文は空白です。

4

1 に答える 1

1

理由はわかりませんが、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');
于 2012-09-14T16:01:27.100 に答える